How to properly stop AIPath when destination unreachable?

Hello!

I have a pretty basic situation where the AI’s destination is updated to the Player’s current position. Path recalculation is set to dynamic @ 0.5s and 10 sensitivity. Player’s position changes every frame, but the AI won’t be able to reach it:

image

The problem is, I don’t think any of the available bools (reachedDestination, reachedEndOfPath, pathPending etc.) work reliably to hint that the AI should just give up.

I expected this to work:

void Update()
{
  agent.destination = new Vector3(targetPosition.value.x, targetPosition.value.y, agent.position.z);
  
  if (agent.reachedDestination || (!agent.pathPending && agent.reachedEndOfPath))
  {
  	EndAction(true); // I really need this to happen or the AI will get stuck
  }
}

in this case, sometimes the agent.reachedEndOfPath is still true from a previous path, because the just setting the destination doesn’t clear this flag.

I thought I could just add SearchPath() after setting the destination but in that case “pathPending” will understandably always return true, and the AI will get stuck.

If I remove the pathPending check, then it just ends prematurely when I start a new path immediately after an old one.

Is there something I’m missing? Or do I have to just set these flags manually (which unfortunately has to be done from the inside cause they don’t have setters)?

Hi

You can check if there is a path between two points using the PathUtilities.IsPathPossible method.

See https://arongranberg.com/astar/docs/pathutilities.html#IsPathPossible

The AIPath script itself never calculates this information itself because it never needs it.