Agents getting stuck on navmesh edges

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:

3156449--240071--pathing-bug.gif

1 Like

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.

private IEnumerator<float> SolveStuck()
{
    Vector3 lastPosition = Vector3.one * -10000;
    float lastDistanceToTarget = m_mobile.RemainingDistance();

    while (true)
    {
        if(m_inFormation)
        {
            if ((m_leader == MapObject) && (m_mobile.RemainingDistance() > m_mobile.StoppingDistance()))
            {
                float distanceToTarget = m_mobile.DistanceToTarget();
                if (lastDistanceToTarget - distanceToTarget < 2.0f)
                {
                    m_mobile.DisableAvoidance();
                    m_mobile.MoveTo(m_mobile.CurrentTargetPosition(), Vector3.zero);
                }
                else
                    m_mobile.EnableAvoidance();

                lastDistanceToTarget = distanceToTarget;
            }
        }
        else
        {
            Vector3 travelled = (lastPosition - MapObject.transform.position);
            if (Mathf.Abs(travelled.x) + Mathf.Abs(travelled.z) < 2.0f)
            {
                m_mobile.DisableAvoidance();
                m_mobile.MoveTo(m_mobile.CurrentTargetPosition(), Vector3.zero);
            }
            else
                m_mobile.EnableAvoidance();

            lastPosition = MapObject.transform.position;
        }

        yield return Timing.WaitForSeconds(1.5f);
    }
}
3 Likes

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.

I am using 2017.1p02 and I still had this issue once in a while, not sure if this is really fixed.

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 :slight_smile:

I seem to have some similar problem, see the video:

repository of this the project is here: https://github.com/ivandonetsunity3d/Kshatriya3d

1 Like

We have a work-around for this bug now: we detect the agent changing direction like this and repath. Not ideal, but it works.

1 Like

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.

Still can reproduce at 2017.3 (I believe the issue section said this was fixed in 2017.2)

I’ve had this issue and fixed it by not setting the " NavMeshAgent.SetDestination(player.position); " at the Start function.

Terrible bug, I duct taped it by using NavMeshAgent.ResetPath() and NavMeshAgent.SetDestination() every 5 seconds.

2 Likes

I also have this problem, the above solution doesn’t work for me.

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.

Are you still using this in 2021? looks fine but idk if still required to avoid bugs

Afaik this is not necessary anymore as those bugs have been fixed.

I still have this problem

surely not fixed in 2020.3
we are getting loads of reports from QA about getting stuck on navmesh… :frowning:

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…

omg… still in Unity 2023.1 w Nav AI 1.1.4 version.

I’m having similar issues with agents on a mesh with areas with different costs: NavMesh agents get stuck on vertices between areas

It seems to have something to do with the agent moving onto an other area before passing one of the nodes in their path? Since they think they missed the node they try to back out of the area, but overshoot and get stuck in a loop like this.