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

NavMesh stopping distance issue

Discussion in 'Navigation' started by solitoracid, Jan 1, 2017.

  1. solitoracid

    solitoracid

    Joined:
    Jul 12, 2015
    Posts:
    2
    Hello everyone. I want to ask for help. You see, I'm working in a "click-to-move" type of game, and I want my character to stop a few meters away from my "target object". So I'm using a piece of code like this:

    public NavMeshAgent playerAgent;

    public virtual void MoveToInteractuar(NavMeshAgent playerAgent)
    {
    this.playerAgent = playerAgent;
    playerAgent.stoppingDistance = 3f;
    playerAgent.SetDestination(this.transform.position);
    Interact();
    }

    My character actually stops a few steps before the target object, but a few seconds later it starts moving towards the target object. How do I prevent this from happening?

    Thanks!

    ps: My apologizes if I spell something wrong, english is not my native language.
     
  2. jonlundy

    jonlundy

    Joined:
    Apr 15, 2016
    Posts:
    25
    Check out the most recent tutorial series on Adventure games on the unity learn site. They discuss this in the first two videos. What they seem to be doing is using the nav mesh agent for most of the movement but then within a certain distance they take over the movement and snap it to the desired location.
     
    witness_ and solitoracid like this.
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,472
    Have you solved problem? How?
     
  4. Tanoshimi2000

    Tanoshimi2000

    Joined:
    Feb 10, 2014
    Posts:
    46
    I'm also having a similar issue. No matter what I set the stopping distance to, the AI reaches the destination completely.
     
  5. robinhood89

    robinhood89

    Joined:
    Apr 6, 2020
    Posts:
    5
    I'm still pretty new at c# and Unity, but I got this to work for me. However, my game is turn-based, so units have a set amount of distance they can move in a turn.

    I click to move, then have the units automatically stop after they've gone the max distance allowed in a turn. I had the script calculate how long they've been moving and therefore how far they traveled, then had the movement shut down once it had gone as far as I wanted.

    if (mRunning == true)
    {
    distMoved += 10 * Time.deltaTime;
    }
    if (distMoved >= maxDistance)
    {
    mNavMeshAgent.isStopped = true;
    mRunning = false;
    rb.angularVelocity = angVelocity;
    rb.velocity = Vector3.zero;
    }
     
  6. MatVic

    MatVic

    Joined:
    Aug 13, 2020
    Posts:
    2
    The stoppingDistance property set the range within the NavMesh stop the movement. As the manual saies, "It is seldom possible to land exactly at the target point". If you need a more accurate positioning, you have to identify when the navigation of your character is stopped and then adjust the position as needed (remember to set isStopped to false for your NavMeshAgent... and also the enable property to false if the object continues to be too far away from your target).
    You have to calibrate well the stoppingDistance property and "your more accurate distance" in order to get a better positioning.
    See here.

    You can use the remainingDistance property of NavMeshAgent to get the remaining distance from your target and compare this value with the stoppingDistance value:

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (navMeshAgent.enabled)
    4.     {
    5.         bool bDistance = navMeshAgent.remainingDistance > navMeshAgent.stoppingDistance;
    6.         if (!bDistance)
    7.         {
    8.             navMeshAgent.isStopped = true;                  
    9.             m_bFixPosition = Vector3.Distance(navMeshAgent.nextPosition, target.position) > distance;
    10.             if (m_bFixPosition)
    11.                 navMeshAgent.enabled = false;
    12.         }
    13.         else
    14.         {
    15.             m_bFixPosition = false;
    16.             navMeshAgent.isStopped = false;
    17.             navMeshAgent.SetDestination(target.position);
    18.         }
    19.     }
    20. }
    21. ...
    22. // In an another method, for example OnAnimatorMove()
    23. void OnAnimatorMove()
    24. {
    25.     if (m_bFixPosition)
    26.     {
    27.         if (Vector3.Distance(m_Rigidbody.position, target.position) > distance)
    28.             m_Rigidbody.MovePosition(Vector3.Lerp(m_Rigidbody.position, target.position, Time.deltaTime));
    29.         else
    30.         {
    31.             m_bFixPosition = false;
    32.             navMeshAgent.enabled = true;
    33.         }
    34.     }
    35. }
     
  7. germano-game-dev

    germano-game-dev

    Joined:
    Feb 25, 2022
    Posts:
    1
    Hi there, hope you all doing ok.

    I just recently started to make a resource gathering game and decided to use NavMesh for handling the AI with State Machine pattern implement by myself for handling the units movement. With a lot of movement here and there with my attacking units I ran into this problem. I did some tests but didn't debug anything yet and I found that is certain that Agent.SetDestination does not work correctly with Agent.stoppingDistance. I'm certain that I'm changing the variables in the correct order but the movement does not work as desired. Making your own movement with Aget.Move guarantees that the agent will stop at the given distance.

    Code (CSharp):
    1. public IEnumerator MoveTo(Vector3 position) {
    2.     // move step by step until reaches the distance
    3.     while (Vector3.Distance(transform.position, position) > stoppingDistance) {
    4.         Vector3 _dir = (position - transform.position).normalized;
    5.         agent.Move(_dir * Attacker.speed * Time.deltaTime);
    6.         yield return new WaitForEndOfFrame();
    7.     }
    8. }
    As soon as I have time to debug the code I'll try to come back and update with more results and if I prove that is a bug with NavMesh components I'll notify them in their master repository (https://github.com/Unity-Technologies/NavMeshComponents).
     
  8. Meta_Bird

    Meta_Bird

    Joined:
    Apr 25, 2020
    Posts:
    16
    @germano-game-dev Did you manage to make any progress with this?

    I am facing this issue when using Agent.SetDestination. But using your fix worked, thanks!

    I can see that, for example, when the agent.remainingDistance is 10 and the agent.stoppingDistance is 1, somehow agent.remaingDistance is less than agent.stoppingDistance.
     
    Last edited: Mar 24, 2022