hasPath false on reachedEndOfPath?

Got a small question that I can’t seem to figure out. I have a task system, so every time the AI get somewhere, I give a callback. And than the AI get the next position from the task manager.

I’m using ai.destination = targetPosition;

Problem is, once the first path is finished, ai.hasPath keeps giving true. I would like to “clear” the path once arrived. That way, the AI doenst keep looking (He is allready on the target) and I can start a new path once one is availeble in my taskmanager.

I also tried checking ai.reachedEndOfPath on an update, but of course it takes a few frames to calculate, so the check in my update keeps giving true, from the previous path.

Any help would be appreciated

Thanks

Hi

If you want to stop the AI from following its current path I would recommend something like

if (ai.reachedDestination) ai.isStopped = true;

In contrast to reachedEndOfPath the newer reachedDestination property does not take a few frames to update.

The above will not technically clear the path, but when the agent is stopped it will not try to follow it in any way. To save some CPU power you could also set ai.canSearch = false.

Make sure you are using 4.2.6 (see https://www.arongranberg.com/astar/download) as that fixes a bug with reachedDestination.

Thank you so much for the very fast reply, I’m going to try it out

edit
Amazing, reachedDestination works like a charm :smiley:

1 Like

The task system works very good now thanks to reachedDestination.

A small additional question:
I used ai.isStopped = true; and that works just fine. But I’am still getting “Path completed” Messages in my Console. I was in the understanding that it would also kill the “Pathfinding”

Is there another way to do this?

If you also want to make it stop recalculating the path you can set ai.canSearch to false.
The isStopped property will just make it stop following the path but it will continue to calculate paths so that it can react quickly if it were to be ordered to start moving again.

Very nice, thank you!