Search Unity

[SOLVED]How can I stop Navmesh agent sliding in Unity5?

Discussion in 'Navigation' started by TakaakiIchijo, Jun 10, 2015.

  1. TakaakiIchijo

    TakaakiIchijo

    Joined:
    Mar 16, 2013
    Posts:
    9
    Hi, Im trying to make Pause function on my enemies (without changing timescale).

    Those characters are attached NavMeshAgent component, and using SetDestination() to following player.

    When the game is paused, I called function "NavMeshAgent.Stop(true)" in Unity 4.3 .

    The (true) option named "stopUpdates" means stop immediately NavMeshAgent without sliding.
    http://docs.unity3d.com/412/Documentation/ScriptReference/NavMeshAgent.Stop.html

    But this option is deleted later than Unity 5, I guess.


    Somebody said change setting acceleration value to higher, but it's dosen't work.

    http://answers.unity3d.com/questions/236828/how-can-i-stop-navmesh-agent-sliding.html

    And I tried using "NavMeshAgent.updatePosition = false".
    Then enemy character stopped, but the NavMeshAgent component was keeping moving separate from parent Object(enemy character model).

    http://forum.unity3d.com/threads/an...stopping-could-you-please-have-a-look.180623/

    So when I set "NavMeshAgent.updatePosition = true", the enemy character warp to unintentional position.

    Please tell me how to solve this issue.
    SFMPE.
     
    NamMap, Harinezumi and williammotta like this.
  2. TakaakiIchijo

    TakaakiIchijo

    Joined:
    Mar 16, 2013
    Posts:
    9
    Through many trial and error, I found solution finally.

    Set velocity Vector3.zero before call Stop();.

    navMeshAgent.velocity = Vector3.zero;
    navMeshAgent.Stop();

    http://docs.unity3d.com/ScriptReference/NavMeshAgent-velocity.html

    If it will be used as reference for someone.


     
    SuperPox, Xx_hugo_xX, madeer and 15 others like this.
  3. fumangy

    fumangy

    Joined:
    Mar 28, 2013
    Posts:
    17
    Thanks !!!
     
  4. rickygow

    rickygow

    Joined:
    Jan 9, 2015
    Posts:
    1
    YOU MY FRIEND, ARE THE REAL MVP
    need hours to find this.
     
    hypermatt likes this.
  5. mbue

    mbue

    Joined:
    Jan 19, 2017
    Posts:
    5
    Thank you - I also searched for that like half a day :)
     
  6. gshong33

    gshong33

    Joined:
    Jan 8, 2018
    Posts:
    1
    Thank you very much
     
  7. hypermatt

    hypermatt

    Joined:
    Jan 27, 2019
    Posts:
    2
  8. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    Thank you! I've been searching for this solution for ages! One minor addition,
    NavMeshAgent.Stop()
    is now deprecated, and
    NavMeshAgent.isStopped = true
    should be used instead (the naming is horrible, it should only be a
    getter
    ).

    Additionally, if you want to restore the movement of the agent after pausing, store the velocity in a variable before setting it to
    Vector3.zero
    , and check for
    agent.isStopped == true
    before restoring it.

    Example:
    Code (CSharp):
    1.  
    2.     IEnumerator MovementCoroutine() {
    3.         agent.isStopped = false;
    4.         Vector3 unpausedSpeed = Vector3.zero;
    5.         while (!WithinRange(target, Range)) {
    6.             if (GameTime.Paused)
    7.             {
    8.                 // emulate effect of Time.timeScale = 0
    9.                 agent.velocity = Vector3.zero;
    10.                 agent.isStopped = true;
    11.             }
    12.             else
    13.             {
    14.                 // restore velocity from before pause
    15.                 if (agent.isStopped)
    16.                 {
    17.                     agent.isStopped = false;
    18.                     agent.velocity = unpausedSpeed;
    19.                 }
    20.                 agent.SetDestination(target.position);
    21.                 agent.speed = originalSpeed / GetNavMeshCost();
    22.                 unpausedSpeed = agent.velocity;
    23.             }
    24.             yield return null;
    25.         }
    26.  
    27.         agent.isStopped = true;
    28.         movementCoroutine = null;
    29.     }
     
  9. NairelPrandini

    NairelPrandini

    Joined:
    Mar 31, 2018
    Posts:
    10
    I found a better solution, just do
    Agent.velocity = Agent.desiredVelocity;
    in the update method. This will ignore aceleration and make the agent go to the target with no sliding or overshooting the target
     
    GeonPang and Yuexien0517 like this.
  10. GeonPang

    GeonPang

    Joined:
    Oct 4, 2021
    Posts:
    2
    Thank you, I solved it simply by doing this.