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
     
  2. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    138
    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.  
     
  3. Waz

    Waz

    Joined:
    May 1, 2010
    Posts:
    252
    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:
    138
    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 Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,469
    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. IndubhushanDas

    IndubhushanDas

    Joined:
    Feb 19, 2014
    Posts:
    110
  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.
     
  8. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,875
    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:
    173
    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.