RichAI properly check when a destination is reached

Hello,

I have a movement script that gets enabled and disabled depending on the current state of the AI. So the first time I use the script and check

     private void Update()
       {
         _aStarAi.destination = target.position;
        
        if (_aStarAi.reachedEndOfPath)
        {
           Debug.Log("Destination reached!");
            IsCompleted = true;
        }
       else
       {
        Debug.Log("Not reached!");
       }
     }

Seems to be working but then I go in another state and at some point I enabled the movement script again and it immediately prints “Destination reached” and after a less than a second it starts printing “Not reached” which makes me think it’s taking awhile to calculate the new path and prints Destination reached using the previous path. So how do I fix that? I have tried adding another check for “pathPending” but it still doesn’t work.

EDIT : I think it has something to do with the RepathRate, the bigger that value is the longer the AI circles around it’s old path and prints “Destination Reached”. But even if it’s 0 it still prints “Destination Reached” once or twice which is not something I’d like because when a destination is reached the movement script is disabled automatically.
Is there a way to “clear” the most recent path when disabling the movement script so next time it’s enabled it will start fresh without having a calculated path (with the previous player’s position which is no longer relevant).

EDIT 2 : It seems like doing this inside OnEnable fixes the issue, but I am still waiting for the developer to respond to make sure it’s the right way to do it.

        _richAi.destination = target.position;
        _richAi.SearchPath();

Hi

Sorry for the late answer.

reachedEndOfPath will tell you if the AI has reached the end of the path it is currently traversing. Setting the destination does not immediately update the path, and even calling SearchPath is an asynchronous call which does not immediately update the path. Therefore the docs recommend that this check is done using something like:

IEnumerator Start () {
    ai.destination = somePoint;
    // Start to search for a path to the destination immediately
    // Note that the result may not become available until after a few frames
    // ai.pathPending will be true while the path is being calculated
    ai.SearchPath();
    // Wait until we know for sure that the agent has calculated a path to the destination we set above
    while (ai.pathPending || !ai.reachedEndOfPath) {
        yield return null;
    }
    // The agent has reached the destination now
}

As you have found this is also discussed in: OnTargetReached() wrong

I’ve been having issues figuring out when A* has actually reached it’s destination and setting ai.destination does not set ai.pathPending to false so this is not a good test to see if it has reached the destination. I’m using RVO controller and simulation, and this destination logic is broken

@rebelbinary

The more up-to-date recommendations are to use ai.reachedDestination instead, which is usually much more closely aligned with what you want.