Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NavMesh - Off Mesh Link - Velocity Control

Discussion in 'Navigation' started by CLX_Software, Jul 17, 2018.

  1. CLX_Software

    CLX_Software

    Joined:
    May 6, 2018
    Posts:
    2
    I am using off mesh links to move an object between my nav meshes. However, I cannot seem to control the speed at which it crosses. I wish it to move at the same speed I have configured for my agent. I do not have animations. The movement itself is the operator sees. (Note. This is not for a game, but more of a simulation software)

    The only thing I have seen that had some impact was to do the following:
    Code (CSharp):
    1.         if (agent.isOnOffMeshLink)
    2.         {
    3.             agent.speed = baseSpeed / 2;
    4.             Debug.Log(agent.velocity);
    5.         }
    6.         else
    7.         {
    8.             agent.speed = baseSpeed;
    9.         }
    This is still not a smooth solution. Is there any way to accomplish my goal?
     
    Last edited: Jul 17, 2018
    MilenaRocha and AntonioModer like this.
  2. havchr

    havchr

    Joined:
    Jun 18, 2009
    Posts:
    75
    You could handle the OnOffMeshLink movement yourself - turn off Auto Traverse Off Mesh link in the inspector of the
    Here is some Pseudo-code - the idea is to kick-off a co-routine to handle moving across a mesh link.

    Code (CSharp):
    1.  
    2.  
    3. if(agent.isOnOffMeshLink && !MoveAcrossNavMeshesStarted){
    4.     StartCoRoutine(MoveAcrossNavMeshLink());
    5.     MoveAcrossNavMeshesStarted=true;
    6. }
    7.  
    8. IEnumerator MoveAcrossNavMeshLink()
    9. {
    10.         OffMeshLinkData data = agent.currentOffMeshLinkData;
    11.         agent.updateRotation = false;
    12.  
    13.         Vector3 startPos = agent.transform.position;
    14.         Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
    15.         float duration = (endPos-startPos).magnitude/agent.Velocity.magnitude;
    16.         float t = 0.0f;
    17.         float tStep = 1.0f/duration;
    18.         while(t<1.0f){
    19.             transform.position = Vector3.Lerp(startPos,endPos,t);
    20.             agent.destination = transform.position;
    21.             t+=tStep*Time.deltaTime;
    22.             yield return null;
    23.         }
    24.         transform.position = endPos;
    25.         agent.updateRotation = true;
    26.         agent.CompleteOffMeshLink();
    27.         MoveAcrossNavMeshesStarted= false;
    28.  
    29. }
     
  3. CLX_Software

    CLX_Software

    Joined:
    May 6, 2018
    Posts:
    2
    Thanks. This works for me.
     
  4. lundquistlaban

    lundquistlaban

    Joined:
    May 12, 2020
    Posts:
    1
    wouldn't this only work for one way though?
     
  5. havchr

    havchr

    Joined:
    Jun 18, 2009
    Posts:
    75
    It has been a while since I wrote the pseudo-code , but I would assume it would work both ways - or if not , you have to do tweaks to data.endPos. it depends how Unity has set up the system. The best way is to do a test and see what happens.
     
  6. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    Thanks - this works really well, both ways. You might want to store agent.destination in a temp Vector3 before starting the transition and reapply after completing the link, so it can continue on to whatever it's original destination was.