A* Pathfinding Project

reachedEndOfPath never false


#1

I am using this to move my agents. It works the first time i call it. But after that the reachedEndOfPath stays true forever even if i call the method again with a new destination far away. Is there any way to reset the reachedEndOfPath to false?

IEnumerator GoToDestination(Vector3 targetDestination, Action callback)
{
    Debug.Log(targetDestination);
    aILerp.canMove = true;
    aILerp.destination = targetDestination;
    while (!aILerp.reachedEndOfPath || aILerp.pathPending)
    {
        yield return null;
    }
    aILerp.canMove = false;
    callback();
}

#2

Hi

There are some gotchas with reachedEndOfPath and pathPending. The reason it returns true there is because while you have set the destination, the agent has not started to recalculate a new path yet (which can be forced using ai.SearchPath or by waiting until the next scheduled path recalculation) and so pathPending will be false and reachedEndOfPath will be true because it has indeed reached the end of the current path.

To avoid these issues I would recommend that you use the newer ai.reachedDestination property instead which is much nicer to use.

ai.destination = whatever;
while (!aiLerp.reachedDestination) yield return null;

#3

Hello

Thanks for the explanation and help!

I changed to reachedDestination instead and after changing to a higher value at line 115 in aiLerp it worked great.
With 0.05 it never got close enough to the destination. Instead the agent stopped a little to early and never arrived to the destination. I do not know why. I do not have any physics etc hindering the agent from going all the way.