Search Unity

Agents getting stuck on navmesh edges

Discussion in 'Navigation' started by kaffiene, Jul 24, 2017.

  1. kaffiene

    kaffiene

    Joined:
    May 26, 2013
    Posts:
    21
    Using the new navmesh in Unity (5.6.0) we're getting a bug where agents are getting caught on the corners of nav mesh cells. They end up moving back and forth on the spot and not progressing towards their target.

    This gif shows the problem in action:

    pathing-bug.gif
     
    kyuskoj likes this.
  2. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    Have you tried to play around with Acceleration and Speed of the NavMesh agent? I had similar problems, but I fixed it with setting these values:

    Speed = Value;
    Acceleration = Value * 1,25;

    If you increase timescale it still might happen sometimes, also set autoBreaking = true and autoRepath = true.

    I have also written some code for my formations to prevent getting stuck in those loops. Please note that the code does not use the NavMesh class. If you have problems understanding the code let me know, and I will explain what I am doing there.

    Code (csharp):
    1.  
    2.  
    3. private IEnumerator<float> SolveStuck()
    4. {
    5.     Vector3 lastPosition = Vector3.one * -10000;
    6.     float lastDistanceToTarget = m_mobile.RemainingDistance();
    7.  
    8.     while (true)
    9.     {
    10.         if(m_inFormation)
    11.         {
    12.             if ((m_leader == MapObject) && (m_mobile.RemainingDistance() > m_mobile.StoppingDistance()))
    13.             {
    14.                 float distanceToTarget = m_mobile.DistanceToTarget();
    15.                 if (lastDistanceToTarget - distanceToTarget < 2.0f)
    16.                 {
    17.                     m_mobile.DisableAvoidance();
    18.                     m_mobile.MoveTo(m_mobile.CurrentTargetPosition(), Vector3.zero);
    19.                 }
    20.                 else
    21.                     m_mobile.EnableAvoidance();
    22.  
    23.                 lastDistanceToTarget = distanceToTarget;
    24.             }
    25.         }
    26.         else
    27.         {
    28.             Vector3 travelled = (lastPosition - MapObject.transform.position);
    29.             if (Mathf.Abs(travelled.x) + Mathf.Abs(travelled.z) < 2.0f)
    30.             {
    31.                 m_mobile.DisableAvoidance();
    32.                 m_mobile.MoveTo(m_mobile.CurrentTargetPosition(), Vector3.zero);
    33.             }
    34.             else
    35.                 m_mobile.EnableAvoidance();
    36.  
    37.             lastPosition = MapObject.transform.position;
    38.         }
    39.  
    40.         yield return Timing.WaitForSeconds(1.5f);
    41.     }
    42. }
    43.  
     
    IndieFist, abdallatawfik14 and akuno like this.
  3. Waz

    Waz

    Joined:
    May 1, 2010
    Posts:
    287
    This is a known bug. It is fixed in 2017, but not yet backported, though I've been told it will be. While changing some settings will make it less likely, it cannot be avoided, so I'd suggest not releasing a game based on 5.6 until this is fixed because eventually it will happen.
     
  4. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    I am using 2017.1p02 and I still had this issue once in a while, not sure if this is really fixed.
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    People often will make mistakes about bugs, unity gets it all the time from people who say "oh but you said my bug would be fixed" but it turns out what they thought was their bug wasn't, because they didn't file a bug report and assumed they didn't.

    So if in doubt always: a) assume its not fixed until you read your particular case numbers in fixed notes b) assume it will never be fixed if didn't send them a case :)
     
  6. IvanDonets

    IvanDonets

    Joined:
    Feb 19, 2014
    Posts:
    117
    devbot_amata likes this.
  7. kaffiene

    kaffiene

    Joined:
    May 26, 2013
    Posts:
    21
    We have a work-around for this bug now: we detect the agent changing direction like this and repath. Not ideal, but it works.
     
    akuno likes this.
  8. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,977
    For now we calulate the path using the agent but we dont use the agent for movement and do our own movement, this helps. You must recheck the path more often than needed though at the moment as sometimes a path returns as invalid when it is not, for some bogus reason re-querying will return the valid path.
     
  9. Athomield3D

    Athomield3D

    Joined:
    Aug 29, 2012
    Posts:
    193
    Still can reproduce at 2017.3 (I believe the issue section said this was fixed in 2017.2)
     
  10. HiiAmHumming

    HiiAmHumming

    Joined:
    Aug 19, 2017
    Posts:
    1
    I've had this issue and fixed it by not setting the " NavMeshAgent.SetDestination(player.position); " at the Start function.
     
  11. akuno

    akuno

    Joined:
    Dec 14, 2015
    Posts:
    88
    Terrible bug, I duct taped it by using NavMeshAgent.ResetPath() and NavMeshAgent.SetDestination() every 5 seconds.
     
    anycolourulike and Athomield3D like this.
  12. WardPeeters

    WardPeeters

    Joined:
    Mar 19, 2015
    Posts:
    25
    I also have this problem, the above solution doesn't work for me.
     
  13. MrBIoBR

    MrBIoBR

    Joined:
    Jul 2, 2017
    Posts:
    9
    I had the same problem here (unity 2019.3.10)
    and ended up making a work around:
    Disable the agent.update rotation and agent.update position, move the character by using rigidbody.movePosition
    and than, just 'updates' the agent position with : agent.nextPosition(rigidbody.position)

    for my game it works fine, since I'm controlling the player, and this lets me have a more fluid control over it.
     
  14. IndieFist

    IndieFist

    Joined:
    Jul 18, 2013
    Posts:
    520
    Are you still using this in 2021? looks fine but idk if still required to avoid bugs
     
  15. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    Afaik this is not necessary anymore as those bugs have been fixed.
     
  16. ensn8

    ensn8

    Joined:
    Sep 29, 2020
    Posts:
    1
    I still have this problem
     
  17. RZGames_Jethro

    RZGames_Jethro

    Joined:
    Jul 6, 2017
    Posts:
    88
    surely not fixed in 2020.3
    we are getting loads of reports from QA about getting stuck on navmesh... :(
     
  18. tormentoarmagedoom

    tormentoarmagedoom

    Joined:
    Jun 17, 2016
    Posts:
    4
    Exactly same problem. version 2022.1.0b ....

    Nav mesh agent going arround to nav mesh surface node point... As all my agents go in 1 single direction, i can "easy detect" when they are weird, but its consuming me a lot of CPU as i have hundreds of units...
     
  19. kyuskoj

    kyuskoj

    Joined:
    Aug 28, 2013
    Posts:
    56
    omg.. still in Unity 2023.1 w Nav AI 1.1.4 version.
     
  20. teunververs

    teunververs

    Joined:
    May 22, 2015
    Posts:
    27