Determine NavMeshAgent has completed its path.

Hi,

What’s the correct way to determine that NavMeshAgent has finished? I’ve tried a variety of approaches and I assumed that I’d have to combine a number of different things, but I can’t find any combination or approach which always works.

NavMeshAgent.remainingDistance - this can sometimes still be quite high, so while this reaching or nearing zero is a sign it’s finished, I need something else for the occasions when it never nears zero.

NavMeshAgent.pathStatus - this becomes PathComplete as soon as I give the agent a new target, despite the fact that it has not even or barely started moving yet, so this is no help.

NavMeshAgent.velocity - same as pathStatus. This is zero as soon as I send the NavMeshAgent going, so checking this for zero isn’t going to tell me anything.

I’ve also looked at speed, hasPath and pathStale, but these don’t seem to provide any useful information either.

Presumably there must be some absolute method to determine when a NavMeshAgent has either reached his destination, given up on trying to reach his destination or just generally got as close as he’s going to get?

Any suggestions?

I manually check the distance to destination and consider it done when it’s below some minimum.

For example:

if (Vector3.Distance(transform.position, agent.destination) <= 1f)
{
// I’m there!
}

The NavMeshAgent is flakey and poorly document. I use it, but trust it for very little.

4 Likes

I use the following code to find out if I’ve reached the end of a path.

Note: The ‘hasPath’ member should be set to false each time a new agent destination is requested.

    public float pathEndThreshold = 0.1f;
    private bool hasPath = false;
    bool AtEndOfPath()
    {
        hasPath |= agent.hasPath;
        if (hasPath && agent.remainingDistance <= agent.stoppingDistance + pathEndThreshold )
        {
            // Arrived
            hasPath = false;
            return true;
        }

        return false;
    }
1 Like

Sorry for the very late necro bump but I have never seen a “|=” operation before.

How does the operator work? And what exactly does this operator do?

And I suppose would this operation below from the example work within JavaScript, because I am currently getting an error.

  if (hasPath  agent.remainingDistance <= agent.stoppingDistance + pathEndThreshold )

I’ve been looking for code like this so I can increment the index of the waypoints so I can set the next destination.

Its an OR assignment operator (see Boolean logical operators - the boolean and, or, not, and xor operators - C# reference | Microsoft Learn). hasPath is only false when both itself and agent.hasPath are false.

Not sure that code will work in its entirety… I’m guessing the original author meant that to say

if (hasPath &&  agent.remainingDistance <= agent.stoppingDistance + pathEndThreshold )
0000 0010   =|   0010 0010     is     0010 0010

This allows to “merge” the values, enabling both 1st bit and the 7th bit.
Of course, this results in a very different number, but we only care about bit-representation

1 Like

This URL might come handy. http://answers.unity3d.com/answers/746157/view.html
Note :- Don’t get confused by the ‘Best Answer’. Go for the 2nd answer.

1 Like