Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

NavMeshAgent(s) gets stuck when NavMesh is dynamically carved.

Discussion in 'Navigation' started by psilord, May 1, 2015.

  1. psilord


    Feb 13, 2015

    I'm using Unity 4.6.5f1 (but I saw the problem in 4.6.2f1) and I've run into a hard problem with pathfinding.

    Basically, I have a room with a sliding door on it. The door opens for the player and for the enemies.
    However, I can lock the door to prevent enemies from coming into the room. I accomplish this by
    having a disabled NavMeshObstacle on the mesh for the door. When I lock the door, I enable the
    mesh and carving on the mesh. This allows the door to carve the mesh and cuts off all paths from outside the room to inside the room.

    What happens nearly all the time is this:

    When the enemies are chasing the player, the player goes into the room and locks the door.
    What happens next is subtle...

    The enemy stops dead in its tracks because it can't find a path to the interior of the disjoined
    room. Great! After a chase timer I wrote expires (because the enemy can't see the player),
    I give the enemy a new destination (in a patrolling state) that is not in the closed off room. However, the enemy doesn't move. This is surprising.

    To try and figure out of this was my code, I wrote extensive debugging about the internal state of the NavMeshAgent so I can see what's going on and a lot of debugging in my own code. As far as I can tell, my code should be working.

    The state of the NavMeshAgent is that the nextPosition doesn't change (no matter to what I set the destination). PathPending is true, PathStatus is "Path Complete", PathStale is true, HasPath is false.
    AutoRepath is true (and always was true since I don't modify it).

    Now, when I open the door, the NavMeshAgent wakes out of its crazy state and immediately goes
    to whatever last destination I had set for it.

    Ok, here is the subtle part. When I initially lock the door _ALL_ Patrolling NavMeshAgents in the level (even those far away from the player) stop and enter an unmoving state. That is EVEN MORE surprising. When I unlock the door, all NavMeshAgents continue on as if nothing had happened with whatever destination they have active at the time they wake up.

    And more subtlety: if I lock the NavMeshAgent INTO the room, it doesn't seem to cause a problem.
    There is only a problem when I've locked the agent OUT of the room.

    This problem is causing a severe headache for me. I've poked around the forums and found some things which led me to upgrade from 4.6.2 to 4.6.5, but nothing definite. I rebaked my NavMesh when I upgraded.

    About 95% of the time this unmoving state happens when I initially lock the door (which enables the obstacle on the door and carves the mesh) and an enemy is trying to get into the room. The other 5% of the time, it works as intended. I'm not specifically using threads or coroutines, and when this happens the game isn't frozen, just the pathfinding agents are. So, I feel this may be a bug in the NavMeshAgent code inside of Unity.

    Any suggestions would be appreciated.

    Thank you!