Do not calculate path if destination is outside graph or inside an obstacle

I am using a GridGraph and using the AIPath + RVO.

If I set up ai.destination outside the graph the entity will try to reach the position with no errors.
I want to prevent if the ai tries to move outside the graph or into a collider (preconfigured collider2D),
because it has an OnReachDestination method, but it will never reach it.

Saw the recast graph navmeshcutting script, but just moved to 2D GridGraph to a high performance.
Tried IsPathPossible but it is allways returning true.

How could I prevent this?


The easiest way to do this is to adjust the A* Inspector -> Settings -> Max Nearest Node Distance.
The path will fail if it cannot get within that distance to the goal.

Also note that OnReachedDestination is deprecated. I recommend that you use ai.reachedDestination or possibly ai.reachedEndOfPath instead.

1 Like

Thank you, the Max Nearest Node Distance set to 0.1 works perfectly.

I remember now. I am not using ai.reachedEndOfpath because it was telling me allways that path was not reached because of the endReachedDistance was not 0 and whenCloseToDestination is set to Stop.

I am using a simple Corroutine to detect if was stoped near the reached distance instead the destination:

public IEnumerator OnReachDestination(){
    yield return new WaitUntil(() => Vector3.Distance(ai.position, ai.destination) < ai.endReachedDistance + 0.5);
    destinationReached = true;

I missed something.

Given that square, where a is walkable and b is an obstacle.

a a a a  
a b b a  
a b b a 
a a a a

Now, with your help, the ai will “Path Failed” trying to reach b positions.
But the ai should calculate randomly to move inside the zone.

I was wondering that SearchPath was not void and it returns true or false depending if “Path Failed” or not.
So, what is the best mehod to have a response if SearchPath has fail?


I’m not quite sure what you mean. Your message title says “Do not calculate path if destination is outside graph or inside an obstacle”. So I thought you didn’t want it to calculate a path close to those B positions?

I do not want the ai tries to reach an outside graph or obstacle, so this is the reason because of I do not want the ai trying to reach an impossible destination. (Before changing “Max Nearest Distance Node” it tried to go outside the graph stopping near the wall).

But if with a random path calculation inside a square zone it tries to reach an impossible destination, I need to know if the path has failed (like the A*'s return log of “path failed” in the console) to control the errors.

Edit: I am thinking now that this will be not the best solition for that. But I am still wanting how to know to get a “path failed” response.


If you want to see if it is reachable, then you can as you say originally use IsPathPossible.
The reason it always returned true when you tried to check if a path to outside the graph was valid was that it first finds the closest nodes to the start and end point. The closest node to a point outside the graph will be a point inside the graph, and there was probably a valid path to that node. However the Max Nearest Node Distance checks that you added later will also impact this check, so now it will work for you I think.

Another simpler solution perhaps could be to check the last path result using something like seeker.GetPath().error. That will be true if the last path failed.

1 Like

Thank you, now I understand what means Max Nearest Node Distance, but it will be so usefull the GetPath().error.