Search Unity

When lots of nav-mesh-agents they crowd around instead of doing what they are supposed to

Discussion in 'Navigation' started by olliewe88, Jan 27, 2019.

  1. olliewe88

    olliewe88

    Joined:
    Dec 4, 2015
    Posts:
    11
    A simple script is attached to the zombies that make the goto the player. However, when there are lots of zombies that spawn in they all just huddle together and takes them a while to all reroute to the player's location.


    The code for the zombies
    Code (CSharp):
    1.   void Update()
    2.     {
    3.         //nma.NavMeshAgent.destination(target.transform.position);
    4.         nma.SetDestination(target.transform.position);
    5.  
    6.  
    7.     }
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Yeah that's pretty normal. What basically happens is that they try to avoid each other but because each zombie is constantly moving the avoidance pathing fails. Try and spawn them within a short distance of each other, preferrably with a very short (0.1 seconds) time interval between each spawn.
     
    olliewe88 likes this.
  3. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    What you also should do is to randomize the position they are walking towards. It should not be the exact player position but random circle positions, where the radius could depend on the amount of zombie which are following.
    If they all walk toward the same location you avoidance is not going to work properly. Also give each zombie slightly different speed and reaction times, it will also help to mitigate the clustering.
     
  4. olliewe88

    olliewe88

    Joined:
    Dec 4, 2015
    Posts:
    11
    Thanks however they are meant to go to the player, not near them. How would I do this? Thanks
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    1. Use the agent priority
    2. Don't tell them to repath every frame, in fact just set1 destiation per zombie per frame, something like that.
    Read manual for a bit more info.
     
  6. olliewe88

    olliewe88

    Joined:
    Dec 4, 2015
    Posts:
    11
    Thanks, I have made some of the changes as seen in the photo it is better but enemies sometimes walk right next to you and still huddle up (but less frequent)
     

    Attached Files:

  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Priority has to be different for each zombie.
    Scripting still has to stagger setting destinations.