Knowing if a position is within the navmesh

Hello,

Is there any way knowing if a given position (Vector3) is within the active navmesh graph?

Thank you in advance,

Julen.

Hi

It is surprisingly hard to find a definition of “inside” that works for all games.

Some options that you have are:

1
Check if the point is on the navmesh when seen from above

bool isInside = AstarPath.active.data.navmesh.PointOnNavmesh(somePoint, NNConstraint.None) != null;

2
Check if the closest point on the navmesh is close

float distance = Vector3.Distance(somePoint, AstarPath.active.GetNearest(somePoint).position);
if (distance < someThreshold) {
    // Yay, this point is very close to the navmesh
}

3
Check if the closest point on the navmesh is close when seen from above

var nn = NNConstraint.None;
var directionToClosestPoint = AstarPath.active.GetNearest(somePoint).position - somePoint;
directionToClosestPoint.y = 0;
float distance = directionToClosestPoint.magnitude;
if (distance < someThreshold) {
    // Yay, this point is very close to the navmesh when seen from above
}

Also. Depending on what you end up going for, this option may turn out to be useful for you https://arongranberg.com/astar/docs/nnconstraint.html#distanceXZ

2 Likes

Thank you so much @aron_granberg !

Edit:
Option 1 works like a charm for my case, just needs a little correction (.data.):

bool isInside = AstarPath.active.data.navmesh.PointOnNavmesh(somePoint, NNConstraint.None) != null;
1 Like