Find closest point on Grid Graph

Hi! I have an Environment Query System giving me the best point to go to. The trouble is, sometimes the point isn’t on the Grid Graph my character belongs to – it’s often just outside. How do I find the closest point on its graph to the target point? I’m specifically interested in the closest point, but the closest node could also work since they are small enough.

Hi

You can find the closest point using

// Find the closest *walkable* node
var info = AstarPath.active.GetNearest(myPoint, NNConstraint.Default);
var closestPoint = info.position;
var closestNode = info.node;

See also https://arongranberg.com/astar/docs/accessingdata.html

1 Like

Yes, I know this, but I am wondering how I can find the closest walkable point on a specific graph. What does node.Walkable actually mean? Same question about the NNConstraint isWalkable. The same node or point can be walkable on one graph, but not walkable on another one. How do I specify which graph I mean when I’m asking it to find the nearest point?

Hi

You can find a node on a specific graph by supplying a constraint:

var constraint = NNConstraint.None;
// Only find walkable nodes
constraint.constrainWalkability = true;
constraint.walkable = true;
// Only look for nodes on graph index 1
constraint.graphMask = 1 << 1;

var info = AstarPath.active.GetNearest(myPoint, constraint);
var closestPoint = info.position;
var closestNode = info.node;

See also https://arongranberg.com/astar/docs/nnconstraint.html#graphMask

Different graphs have different nodes.

1 Like

I did this, and it works (yay!), but I’ve noticed something else after I implemented this. Quite often, my unit will pick a node right on the edge of the walkable surface of the graph and won’t be able to follow the path there. It’ll stop and keep retrying the path until the destination updates.

I have a Simple Smooth and Funnel modifiers on the character, and they don’t seem to be applied to this path, I assume, because it never gets that far and fails before, so it ends up looking pretty angular. Here’s the situation described on the screenshot:

If I send a unit there on purpose (the other unit on the screenshot is going to a similar point successfully), it seems to get there no problem.

I don’t know if this is directly related or not, but it does end up happening quite often now, and it’s a 100% repro when the point is on the edge like this.

Here’s another one in that same corner:

image

I just noticed that the valid one has a little blue circle in the end, but when it fails like this, the circle is absent.