How To Stop Pathfinding On Target Reached?

I’ve been looking through the forums for code to stop the units current path, but none of it seems too be working in the OnTargetReached method.

Here’s what I’ve added to the method.

	public virtual void OnTargetReached () {
    // The end of the path has been reached.
    // If you want custom logic for when the AI has reached it's destination
    // add it here.
    // You can also create a new script which inherits from this one
    // and override the function in that script
    seeker.CancelCurrentPathRequest();
    seeker.GetCurrentPath().Error();
    path = null;
}

How else should I be attempting to stop repathing over and over once the target has been reached?

Edit: I’ve already logged the event to make sure it’s definitely calling OnTargetReached.

Hi

If you use the 4.1 beta then you can simply do something like this:

void Update () {
    ai.isStopped = ai.targetReached;
    ai.canSearch = !ai.isStopped;
}

Otherwise the you will probably have to set the speed to 0 instead of setting isStopped.

Calling CancelCurrentPathRequest will only make it abort the path it is currently calculating. It will not prevent the AI from calculating paths again in the future. That’s what the canSearch property is for.