Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Obstacle avoidance jittery

Discussion in 'Navigation' started by sunnychan1337, Apr 20, 2021.

  1. sunnychan1337

    sunnychan1337

    Joined:
    Dec 14, 2020
    Posts:
    8
    Hi all,

    First of all I've made a quick video to showcase the issue.

    As you can see the agents have the cube as their destination. Of course all is well when you only have a single agent, but when there's multiple agents who need to encircle their target, the movement gets jittery. I don't understand why this doesn't get talked about more since this should affect(as far as i can tell) every project where you have multiple agents who use the built in obstacle avoidance. Is there a solution to make the movement look a little more fluid?

    Please note that I've already turned off "auto breaking" and the stopping distance is 2.5 and the obstacle avoidance radius is 0.7.

    Now if we could add the navmesh obstacle component to them, and just carve when they reach their destination, the remaining agents would know which areas are occupied and just repath to a free area, thus avoiding a lot of unnecessary bumping, but unfortunately adding the obstacle component doesn't work well when the unit also has the "agent" component attached.
     
    Last edited: Apr 20, 2021
  2. sunnychan1337

    sunnychan1337

    Joined:
    Dec 14, 2020
    Posts:
    8
    So since this subject is a little underdocumented I decided to showcase my band aid solution here as well. You can just attach the Navmesh Obstacle to your agents and disable them. Then, once your agent is in attackrange, you disable the NavMeshagent component and enable the obstacle. Once it's out of attack range you do the opposite. I don't know if this is what you'd call an elegant solution(or is it?) so i'd still love some input from people who know more about the subject than I do. All I can say is that it does work, Enemy AI nicely encircle their target and there is no jitter since they aren't trying to push each other away.
     
  3. sunnychan1337

    sunnychan1337

    Joined:
    Dec 14, 2020
    Posts:
    8
    Update 2 for the people who find this on google in the future:
    It turns out that switching between the navmeshObstacle component and the NavMeshAgent component can cause some teleportation. For example, if you have a couple of agents chasing the player, and the agent in front is in attackrange, he will turn himself into a navmeshObstacle and carve into the navmesh. This can cause the agent behind him to slightly teleport if his unit avoidance radius intersects with the carved box. This looks a bit ugly and unrealistic. Luckily there is an easy fix though. If you take away movement control from your agent by setting
    Code (CSharp):
    1. agent.updatePosition = false;
    then you can control movement yourself with a nice Lerp. Just find a lerp value that works for you. One that does a good job at masking the teleportation but accurate enough.
    Example:
    Code (CSharp):
    1. Vector3 lerp = Vector3.Lerp(this.transform.position, agent.nextPosition, Time.deltaTime * 3);
    And then there's 1 last issue. When you switch from obstacle to agent, it seems the carved patch lingers for a couple of ms even after deactivating the component which causes the agent to teleport. Easily fixed by putting the switching logic into a coroutine where you put a short wait timer.

    Code (CSharp):
    1. obstacle.enabled = false;
    2. yield return new WaitForSeconds(0.001f);
    3. agent.enabled = true;
    A lot of bandaid solutions it seems to me but what you end up with is pathfinding + obstacle avoidance that doesn't jitter, bump into each other needlessly, can surround their target and looks silky smooth.

    The result:
     
    Last edited: Apr 21, 2021
  4. Tomasz_Pasterski

    Tomasz_Pasterski

    Joined:
    Aug 12, 2014
    Posts:
    99
    Nice job man,
    I try and implement this into my RTS project and see how this will work with typical RTS orders like "attack move", wonder how this work.
     
  5. darashayda

    darashayda

    Joined:
    Jun 27, 2020
    Posts:
    440
    Admirable effort.
     
  6. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    Nice approach, but this works only for a certain number of units. There is another problem with switching on and off NavMeshObstacles.

    If you have a certain number of units and switch on/off NavMeshObstacles in almost every frame most of the units will barely move anymore as their path variable gets invalidated time and time again.
     
  7. JohnnyConnor

    JohnnyConnor

    Joined:
    Aug 10, 2020
    Posts:
    42
    I too don't understand how this issue with the NavMesh system wasn't pointed out by almost anyone. It was literally the very first issue I had to deal with when I started using it.

    Thankfully, I managed to solve my problem with this nav mesh hack guide I found earlier in another thread. He used an approach very similar to yours.
    Unfortunately, its images are no longer being hosted and the code sections have been unformatted. Still, just sharing it here in case people are interested in seeing another way of approaching this issue.
     
    GrassWhooper, kader1081 and mbussidk like this.
  8. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    376
    Please some unity staff
    look at this i have wasted about 10 hours just because of this should i change to astarpathfinding. They jitter when they are close each other
     
    Last edited: Aug 6, 2023
    GrassWhooper likes this.