Off mesh links how to know if at start or end of bi directional link the agent is at?

I have an off mesh link and i current am able to detect when the agent is on the off mesh link as part of his path.

But i need to know which end of the off mesh link the agent is at so i can run the correct animation. Since it is a vertical off mesh link, i need to know if the agent has to climb up or climb down. But there does not appear to be any way to know which end of the off mesh link the agent is at according to the available methods and properties?

Am i missing something, how would it be done?

Did you check the NavMeshAgent.path.corners values? I’m not sure how easily you’ll be able to use those to confirm the direction, but the corners should represent the hop between Off Mesh Link endpoints. If it’s always a vertical distance concern, you could see whether the next corner is higher or lower than the current one.

I think you should be able to get the endPos with
agent.currentOfMeshLinkData.endPos and that it’s adjusted for bi-directional - so that you can check .endPos.y against
agent.currenOfMeshLinkData.startPos.y to see if you’re traversing up or down.

1 Like

Sorry for bump this old thread

how can i check in an traverse horizontal, if we are closes from x,z of endpos in a bidirectional link?
I can attach a gameobject in each point and access as external component, but maybe is another way in a link bidrectional to know what point is closest?

I am not understanding quite what you are asking for, a sketch would help.

I know this is an old thread, but if anyone is still looking for an answer, you can check how far the target is from each point (when they enter the link of course).

private bool IsMovingFromStartToEnd(Transform target, NavMeshLink link)
    {
        var linkT = link.gameObject.transform;
        // Get the world position of the start and end points
        Vector3 startPos = linkT.TransformPoint(link.startPoint);
        Vector3 endPos = linkT.TransformPoint(link.endPoint);
        // Compare the squared distances between the target and each point   
        float sqrDistFromStart = (target.transform.position - startPos).sqrMagnitude;
        float sqrDistFromEnd = (target.transform.position - endPos).sqrMagnitude;
        return  sqrDistFromStart <= sqrDistFromEnd;
    }

This simple method will return true if the agent is moving from start to end, or false if it’s moving from end to start.
Also, the “target” is obviously whatever makes sense in your case, but it will most likely be a NavMeshAgent sense that’s typically what you use to move around the Nav Mesh and the Links.

Note: We’re using squared distance because using Vector3.magnitude has a considerably higher cost than using Vector3.sqrMagnitude and since we’re just comparing which one is bigger, we can do it this way.

1 Like