Using PointOnNavmesh but need Y axis check

Hello, I am using Pro version and recast graph.

I’m want to use navmesh whether player can move to a position at next frame or not.
If player can not move to there, then redirect player’s way to closest position with GetNearest().
This is to prevent player jump out from map.(Without boundary collider)

Everything seems to fine except when another floor is exist.


Here’s my scene screenshot GIF.

PointOnNavmesh checks XZ plane but I need check Y too.

I tried using the returned GraphNode from PointOnNavmesh() and check it’s position,
But PointOnNavmesh returned lowest floor node.

How can I check
A position where player wants to go is on navmesh including Y axis?

Hi

I’d recommend checking if the closest node is “close enough” on the Y axis.
Something like

var nn = NNConstraint.Default;
nn.xzDistance = true;
var pos = AstarPath.active.GetNearest(myPosition).position;
var delta = myPosition - pos;
if (new Vector2(delta.x, delta.z).magnitude < 0.1 && Mathf.Abs(delta.y) < 1.0) {
    // On the navmesh
}

This will check if the position is on the navmesh in the XZ direction (well, up to 0.1 units off the navmesh), and in the Y direction (up to 1 meter above or below the navmesh).

However, for your use case, I’d recommend something like this. This is (roughly) what the AIPath script does with its constrainInsideGraph option enabled. The method returns the new position of the agent after being clamped to the navmesh.

protected override Vector3 ClampToNavmesh (Vector3 position) {
    var nn = NNConstraint.Default;
    cachedNNConstraint.distanceXZ = true;
    var clampedPosition = AstarPath.active.GetNearest(position, cachedNNConstraint).position;

    return new Vector3(clampedPosition.x, position.y, clampedPosition.z);
}

Thank you for help!
I tried in similar way before but failed at that time.
However, this time seems different.

Works almost as I wanted.
Thank you.

1 Like