Get nearest node

Hey guys, quick question here… I’m making an RTS/TBS with some obstacles that are a little too cumbersome to not click on…

So I would like to make a system where if you click on a non walkable space, it will find the nearest walkable space and place a waypoint… Is there something I can use to get this?

Something like "GetNearestNode(vector3 point) would work just fine!!

Any help would be great and would save me dumb amounts of raycasting!!

FYI: I’m using a recast(Navmesh style) grid


Sure, that exists.

NNInfo nn = (transform.position, NNConstraint.Default); if (nn.node != null) { Debug.Log ("Found closest node at " + (Vector3)(nn.node.position) + " the closest point on the node was " + nn.clampedPosition); } else { Debug.Log ("No close node found, maybe adjust A* Inspector -> Settings -> Max Nearest Node Distance"); }

Note that if you set the Seeker -> Start End Modifier -> End Point to ClosestOnNode. It will stop the path on the closest point it can reach when you do a path request, I think this is what you are actually looking for.

PS: using NNConstraint.Default makes sure it find the closest Walkable node instead of just any node. Though if you are using a navmesh based graph, there are usually no unwalkable nodes.

Hi Aron,

Was wondering if there was a way to get the next node in your characters path and convert to a Vector 3?

The vectorPath’s (I,E the Vector 3’s in a path) are stored in a generic list *Look that up if you don’t know what it is)… the .vectorPath variable will get you what you want ~

Something like this should get you what you want
Vertor3 NextNode = CurrentPath.vectorPath[ThisNode+1];

awesome thank you!