Closest Path (partial path)?

Hi. I’m developing a game (similar to Baldur’s Gate) and I need the party to go where the player has clicked. But the issue is that even if the player click inside a wall the pathfinder must get the nearest walkable area. Is there a way for the seeker to return the closest path?

(In the image the yellow cross represents the clicked position and the green cross represents the nearest (desired) position)

As you can see, the AstarPath.active.GetNearest() does not work (the way I tested it at least) since the clicked area is “walkable”.

Thanks.

Why is the clicked area walkable?? AstarPath.active.GetNearest() is really your best option~

If you could further explain how you tested it, and why it did not work hopefully I can write up some working code :slight_smile:

The clicked (pink-ish) area should not be walkable, but there are nodes on the pink area. The AstarPath.active.GetNearest() doesn’t know that those nodes(pink graph) shouldn’t be reachable, because it doesn’t know that I only want to search on the blue graph. If I use AstarPath.active.GetNearest() on the yellow cross position it will return the nearest node on the pink graph/area instead of a node on the blue graph.

Hi

This is the default behavior for paths.
Make sure you set seeker->StartEndModifier->EndPoint to ClosestOnNode and it should work well. If you get path failed messages you might need to increase A* Inspector -> Settings -> Max Nearest Node Distance.

Internally this is handled by a GetNearest call with a special NNConstraint.

`
Node a = AstarPath.active.GetNearest(transform.position);

//Make sure the second node is in the same area as the first node
NNConstraint c = NNConstraint.Default;
c.constrainArea = true;
c.area = a.area;

Node b = AstarPath.active.GetNearest (target.position, c);
`

Setting the “A* Inspector -> Settings -> Max Nearest Node Distance” to a higher value did solve the problem (no other changes required). Thanks.