Welcome!

Welcome to our community forums, full of great people, ideas and excitement. Please register if you would like to take part.

This is extra text with a test link..

Register Now

Announcement

Collapse
No announcement yet.

NavMeshAgents and Long Distances

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • NavMeshAgents and Long Distances

    So, I'm having this problem that if my waypoints are a long way from each other, the NavMeshAgent freaks out. What's going on?

    Here's my basic code:
    Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    
    public class HunterAI : MonoBehaviour
    {
        NavMeshAgent myAgent;
        Animator myAnimator;
    
        public Transform[] wayPoints;
        public int nextWayPointNumber;
    
        Vector3 worldDeltaPosition = Vector3.zero;
    
        void Start()
        {
            CleanUpWPGeo();
            myAgent = GetComponent<NavMeshAgent>();
            myAnimator = GetComponent<Animator>();
            myAnimator.SetBool("isWalking", true);
    
            myAgent.updatePosition = false;
    
            SetNewDestination();
        }
    
        void Update()
        {
            if(myAgent.remainingDistance < .5f)
            {
                SetNewDestination();
            }
    
            worldDeltaPosition = myAgent.nextPosition - transform.position;
            if(worldDeltaPosition.magnitude > myAgent.radius)
            {
                myAgent.nextPosition = transform.position + .9f * worldDeltaPosition;
            }
        }
    
        void SetNewDestination()
        {
            if(nextWayPointNumber >= wayPoints.Length)
            {
                nextWayPointNumber = 0; 
            }
            myAgent.SetDestination(wayPoints[nextWayPointNumber].position);
            nextWayPointNumber++;
        }
    
        void OnAnimatorMove()
        {
            // Update position based on animation movement using navigation surface height
            Vector3 position = myAnimator.rootPosition;
            position.y = myAgent.nextPosition.y;
            transform.position = position;
        }
    
    
        void CleanUpWPGeo()
        {
            foreach(Transform wp in wayPoints)
            {
                wp.gameObject.GetComponent<MeshRenderer>().enabled = false;
            }
        }
    
        private void OnDrawGizmosSelected()
        {
            for(int i = 0; i< wayPoints.Length - 1; i++)
            {
                Gizmos.color = Color.green;
                Gizmos.DrawLine(wayPoints[i].position, wayPoints[i + 1].position);
            }
    
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(transform.position, attackRange);
    
            Gizmos.color = Color.white;
            Gizmos.DrawWireSphere(transform.position, chaseRange);
    
            Gizmos.color = Color.green;
            Gizmos.DrawWireSphere(transform.position, gotAwayRange);
    
    
        }
    }

  • #2
    I'm glad you asked that question. Unity has hidden deep within its Manual a section specifically on this (https://docs.unity3d.com/Manual/nav-AgentPatrol.html). And in there they share a secret about:

    Code:
    agent.pathPending
    Basically, this boolean can be checked before actually using SetDestination. The idea is that sometimes it takes a few frames to calculate the new path, and if in those few frames we've already told it to SetDestination to something else, it can yield weird results.

    So, the quick fix is to adjust the Update function so that it reads:
    Code:
        
    void Update()
        {
            if(!myAgent.pathPending && myAgent.remainingDistance < .5f)
            {
                SetNewDestination();
            }
    ...
    So it checks to see if it's busy defining a new path before setting the new destination.

    Comment

    Working...
    X