AI Navmesh Agent path status completed issues

Hello, so I’m thinking my mistake is probably something simple. But I’m making my banana navmesh agent patrol around a map, and once the path is completed, start a Coroutine that will wait a few seconds then send him out again. Issue is that it’s reading the path is complete even when it’s not. SO, I tried removing the Coroutine and just used debug logs to check the status. The way I check this is as follows:

if (bananaAgent.pathStatus == NavMeshPathStatus.PathComplete)
{
    Debug.Log("PATH COMPLETE!");
}

So the agent spawns in, starts his patrol by sending him to a random point, and the above debug log starts going off continuously as if the path is complete even though he’s still moving and hasn’t reached his destination yet. I then tried extending the script a bit more with similar results but a little less continous.

if (bananaAgent.pathStatus == NavMeshPathStatus.PathComplete && bananaAgent.remainingDistance < 2)
{
   Debug.Log("PATH COMPLETE!");
}
else
{
   Debug.Log("Moving");
}

This one ended up going off 10 times really quick at the start that the path was complete, but then transitioned into the moving log until he actually reached the destination. At that point it started saying the path was complete.

At this point the PathComplete part of the function means literally nothing because it doesn’t work as I expect it should so I must be missing something on that. And I’m also not sure why at the start it reads it as complete when it isn’t. He was given a path at the start before the above code even goes off.

1 Like

Okay, so I managed to get it to work properly by switching out the path complete part with path pending like so:

if (!bananaAgent.pathPending && bananaAgent.remainingDistance < 2)
{
    Debug.Log("PATH COMPLETE!");
}
else
{
    Debug.Log("Moving");
}

This works, which means the issue was with the part bananaAgent.pathStatus == NavMeshPathStatus.PathComplete

I’m not sure how the path status is supposed to work, but it doesn’t work based off obvious observation you would expect it to. After a few hours of searching it seems it’s possibly used for the NavMesh itself rather than the agent which is weird as it can be used with agents. Anyone who has a better explanation of what it does I’m all ears.

1 Like

Firstly, you’re going to want to stop using the built-in remainingdistance as you’ll see for yourself, it’s extremely buggy and gets weird values like infinity at times. It will mess up pretty quickly.

You can write your own distance checker, which is a common solution.

However there’s one other trick way to do it. After the agent is moving check if BOTH agent.pathPending and agent.hasPath bools are false. Once they’re both false the agent has definitely finished.

This function works for me:

//  Check if the model is moving on the NavMesh
    public static bool DestinationReached(NavMeshAgent agent, Vector3 actualPosition)
    {
        //  because takes some time to update the remainingDistance and will return a wrong value
        if (agent.pathPending)
        {
            return Vector3.Distance(actualPosition, agent.pathEndPosition) <= agent.stoppingDistance;
        }
        else
        {
            return (agent.remainingDistance <= agent.stoppingDistance);
        }
    }
1 Like