Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Issue Manually Traversing Off Mesh Links

Discussion in 'Navigation' started by Clydey2Times, May 10, 2019.

  1. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    I'm having an issue where an agent object has jerky movement when manually traversing off mesh links. The object (a cylinder in this case) leaves the navmesh agent collider intermittently until control is handed back over to the navmesh agent.

    Here's the code:

    Code (CSharp):
    1. public class NavAgentExample : MonoBehaviour
    2. {
    3.     public AIWayPointNetwork waypointNetwork = null;
    4.     public int currentIndex = 0;
    5.     public bool hasPath = false;
    6.     public bool pathPending = false;
    7.     public bool isPathStale = false;
    8.     public NavMeshPathStatus pathStatus = NavMeshPathStatus.PathInvalid;
    9.     public AnimationCurve jumpCurve = new AnimationCurve();
    10.     private NavMeshAgent _navAgent;
    11.     private void Start()
    12.     {
    13.         _navAgent = GetComponent<NavMeshAgent>();
    14.         if (waypointNetwork == null)
    15.         {
    16.             return;
    17.         }
    18.         SetNextDestination(false);
    19.     }
    20.     void SetNextDestination(bool increment)
    21.     {
    22.         if(!waypointNetwork)
    23.         {
    24.             return;
    25.         }
    26.         int incStep = increment ? 1 : 0;
    27.         Transform nextWaypointTransform = null;
    28.         int nextWayPointIndex = (currentIndex + incStep >= waypointNetwork.wayPoints.Count) ? 0 : currentIndex+incStep;
    29.         nextWaypointTransform = waypointNetwork.wayPoints[nextWayPointIndex];
    30.         if (nextWaypointTransform != null)
    31.         {
    32.                 currentIndex = nextWayPointIndex;
    33.                 _navAgent.destination = nextWaypointTransform.position;
    34.                 return;
    35.         }
    36.         currentIndex++;
    37.     }
    38.     private void Update()
    39.     {
    40.         hasPath = _navAgent.hasPath;
    41.         pathPending = _navAgent.pathPending;
    42.         isPathStale = _navAgent.isPathStale;
    43.         pathStatus = _navAgent.pathStatus;
    44.         if(_navAgent.isOnOffMeshLink)
    45.         {
    46.             StartCoroutine(Jump(1.0f));
    47.             return;
    48.         }
    49.         if ((!hasPath && !pathPending) || pathStatus == NavMeshPathStatus.PathInvalid )
    50.         {
    51.             SetNextDestination(true);
    52.         }
    53.         else if(isPathStale)
    54.         {
    55.             SetNextDestination(false);
    56.         }
    57.     }
    58.     IEnumerator Jump (float duration)
    59.     {
    60.         OffMeshLinkData data = _navAgent.currentOffMeshLinkData;
    61.         Vector3 startPos = _navAgent.transform.position;
    62.         Vector3 endPos = data.endPos + (_navAgent.baseOffset *Vector3.up);
    63.         float time = 0f;
    64.         while(time <= duration)
    65.         {
    66.             float t = time / duration;
    67.             _navAgent.transform.position = Vector3.Lerp(startPos, endPos, t) + (jumpCurve.Evaluate(t) *Vector3.up);
    68.             time += Time.deltaTime;
    69.          
    70.             yield return null;
    71.         }
    72.         _navAgent.CompleteOffMeshLink();
    73.     }
    Is there a conflict of some sort with the agent's update position field?

    Here's an image of how it looks. The object snaps back and forth to the navmesh agent collider.

    navagent.jpg

    Can anyone help me? Kind of at a loss at this point.
     
  2. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    I've tried two separate forums and bumped the threads multiple times. Surely someone can help?
     
  3. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    I don't immediately see anything wrong with how you handle the offmesh link here, but I am very confused by this waypoint system you've built on top of this. Why exactly are you doing this?
    From what I can see you're still letting all movement be handled by your NavMeshAgent and you basically copy over the NavMesh' internal settings into it? That seems like an unnecessary operation that could lead to some conflict cases.

    It could also be possible you're accidentally firing off the jump coroutine multiple times. Try logging something at the start of your coroutine code. If I'm correct there, perhaps store the coroutine in a variable and check if said variable is null before firing it off. At the end of your coroutine, set the variable back to null.