Finding the closest point to an unreachable point?

I’m trying to work out what happens when my pawn tries to navigate to a point it can’t reach. I see that there is a function in Path Utilities for “Is this path possible” and I can use that to determine if the point cannot be reached.

But what I want to do is: when I can’t reach a node, find the nearest node to that node that IS possible, and then make that the destination instead.

So, how do I find where that is? How can I find the nearest point that is possible to reach?

A* inspector => Setting => Max Nearest Node Distance
try change this value, then the Graph.GetNearest() will return the closest node which is inside this distance


This is in fact the default behavior as long as the A* Inspector → Settings → Max Nearest Node distance is high enough.
The system will try to find a path to the node closest to the destination which is reachable from the start point, and which is within max nearest node distance from the destination.

Well that’s pretty cool, but it won’t actually help with my situation. I had to write my own code for moving my pawns, and I use the A* pathfinding to determine which way they should go. As such, it is actually my custom code that determines if they have reached their destination.

So I need a way to access what this closest node is, so I can hook that back into my code. Is there a function I can use to retrieve what was determined to be the ending node?

For reference, what I am currently doing is: I use Seeker.StartPath() to find a path between a nearby point and the target, call Seeker.GetCurrentPath().BlockUntilCalculated();, and then store the value of Seeker.GetCurrentPath().GetTotalLength();. Then I run paths for other possible directions my pawn can go and compare the lengths of the different options, and move in the direction with the shortest length.

The pathfinding my be automatically directing me to the closest node that I can reach, but my pawn doesn’t know that, and just gets stuck trying to walk over the two spaces closest to the nearest node.
The way I see it, either I need to check if the path is blocked, find a new target that is reachable, and set that as my goal, OR I need to retrieve what the path has set as my goal and update my pawn’s target to be that.
I’m not sure which would be easier because I don’t know what functions I have available to call.

Are you using a GridGraph?

In the scenes where this is relevant, yes, I’m using a grid graph.

Ok, so I’m not sure if this is what you’re looking for, but it sounds like your basic problem is that your agent will occasionally (?) attempt to traverse to a position that is not on the graph, ie not walkable, correct?

If that’s the case then there are a bunch of options for solving this problem. What you can start with is calling GetNearest, and passing in an NNConstraint that is walkable. I initially went with this approach, but unfortunately got inconsistent results. Sometimes it would work, but often my agents would get stuck, either on the edge of the graph, or just outside of it.

The solution that I’m using now is to query my graphs for their nodes using GetNodes, and then iterating over the List of Nodes and filtering out any nodes that are not Walkable. This will yield a List of walkable nodes. Once I have that list, I can iterate over it, and create an Array of walkable positions using GraphNode.position. You’ll have to convert the GraphNode positions from Int3 to Vector3 (or Vector3Int), but once you do that your agent can iterate over the Array of walkable positions, and get the nearest position given some arbitrary target position. Nearest position can be discovered by comparing the sqrMagnitude of the distances of your target position from all the walkable positions in your Array (here’s a discussion on how to just that). That may sound like a lot, but you can build that Array of walkable positions in advance, and they’ll be ready to go for your agents until the next time your graph changes.