Search Unity

Question Why is it my NavMesh Agents stop moving when carving NavMesh Obstacles are introduced at runtime?

Discussion in 'Navigation' started by JustTiredOfEverything, May 9, 2023.

  1. JustTiredOfEverything

    JustTiredOfEverything

    Joined:
    Aug 4, 2022
    Posts:
    80
    Hello,

    I'm experiencing a specific issue with NavMesh agents and NavMesh obstacles. In my game, combatants are located on an island, and when they die, their NavMesh agent component is removed, and a rigid body and NavMesh obstacle are added to represent their dead bodies.

    The problem occurs when new NavMesh obstacles are created during runtime, specifically with the "carve" property set to true. With "carve" enabled, agents eventually abruptly stop moving towards their destination, and stay that way even if I explicitly call Set destination afterwards. The obstacle does not seem to need to be near the agent, its path, or destination for this to occur.

    Funny enough, I have other objects in my scene with NavMesh obstacle components and the "carve" property set to true, however they appear to function correctly. I've deduced this is most likely because they exist from the beginning of the scene before the game starts. This leads me to believe that the issue is specifically related to creating NavMesh obstacles with "carve" enabled during runtime, as the pre-existing obstacles do not cause any problems.
     
    KirillEst likes this.
  2. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    As soon as a navmesh obstacle is enabled (with carving) ALL navmesh agents will re-path. This means switching on and off multiple navmesh obstacles in short succession can lead to agents permanently re-pathing.

    This is really bad, but unfortunately, this is how it is currently implemented by Unity. I hope they fix this with the new package but due to the massive current layoffs, I heavily doubt this.
     
    Last edited: Jun 27, 2023
  3. JustTiredOfEverything

    JustTiredOfEverything

    Joined:
    Aug 4, 2022
    Posts:
    80
    Appreciate your input and man, that's a real bummer with Unity's current state. However, I'm still looking for a workaround for this NavMesh obstacle issue. Any suggestions? The game can't really progress without fixing this, and waiting for Unity to pull itself together long enough to acknowledge this bug and make an update might not be feasible. Thanks again!
     
  4. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    I know this may sound like a terrible solution, but in my walking loop what I do is something like:

    //If the agent has found a path, recalculate the path.
    if ( !agent.pathPending) Agent.SetDestination(myDestination.position.PlaceInNav());

    This runs only when the Agent isActiveAndEnable (otherwise you'll get errors). It forces to recalculate the path almost every frame during the time the character is walking. By doing this, it makes sure that the agents won't hit other agents or obstacles, as their path are constantly updating. It may sound crazy, but works fine. And according to some user, it's calculated in a different thread so you don't loose performance of your main trhead (but I wouldn't really know if that's true). In my case, it hasn't created any performance issue, so, I've been using it as a standard solution, and had forgotten ever since.
    Also, remember that when an obstacle is enabled it takes around 2 frames to update the nav mesh, this is kind of a random fact, but since you're using them, it's another one of those things that they don't usually tell you.
     
    Last edited: Oct 19, 2023
  5. hankuk1592

    hankuk1592

    Joined:
    May 30, 2021
    Posts:
    4
    Having same issue with RTS attack