Get GraphNode's normal?

I am clamping the character’s position using the following function, however it would be nice to actually pull character towards the node (it might be heavily inclined). How do I get GraphNode’s normal?

// keeps the vector on the same y-level as the navmesh's surface, and corrects it 
// if it's too far (within horizontal-plane) from the nearest position on the navMesh
protected void ClampToNavMesh(ref Vector3 wanted_nextAi_pos, int graphMask=-1, int tagMask=-1) {
    
    NNConstraint constraint = NNConstraint.Default;

    if(graphMask != -1) {
        constraint.graphMask = graphMask;
    }

    if(tagMask != -1) {
        constraint.constrainTags = true;
        constraint.tags = tagMask;
    }
    //glue to the surface:
    NNInfo info = AstarPath.active.GetNearest(wanted_nextAi_pos, constraint);
    wanted_nextAi_pos.y = info.position.y;

    //discard the user's vector if it's too far, even after the y-correct:
    if((wanted_nextAi_pos - info.position).sqrMagnitude > 0.01f) {  
        wanted_nextAi_pos = info.position;
    }
}

Hi

The normal is not explicitly stored for any node, however if you have a navmesh/recast graph you can calculate the normal like this:

TriangleMeshNode triNode = node as TriangleMeshNode;
var normal = Vector3.Cross((Vector3)(triNode.GetVertex(1) - triNode.GetVertex(0)), (Vector3)(triNode.GetVertex(2) - triNode.GetVertex(0)));

You may also be interested in some methods on the TriangleMeshNode class, such as ClosestPointOnNode, ClosestPointOnNodeXZ and ContainsPoint.

1 Like