Search Unity

Avoid navmesh agent fighting same destination

Discussion in 'Navigation' started by hsjaaa, Apr 30, 2019.

  1. hsjaaa

    hsjaaa

    Joined:
    Apr 30, 2016
    Posts:
    29
    If two or more agent is set same or close destination, they would push each other fighting the space, which is unwanted.
    Although I find sometimes agent push other agent while moving, despite that, avoid agent fighting same destination seems more like a destination or position manage stuff.
    Thinking about offset destination, but the tricky part to me is, how to find the offset destination which is closer to the mouse click point and avoid fighting enemy and teammates destination or position, also offset destination should be within the navmesh.
    Any idea? or could some package help with this?
     
  2. Miggi124

    Miggi124

    Joined:
    Jul 31, 2017
    Posts:
    69
    Hi,

    If you want a simple way for multiple AI to go to the same (or similar location), consider looking into AI flocking behaviours, or formations, but for a simple solution, you can add a random offset every time you set the AI to go to a location, and decrease the width of their NavMeshComponent (I think it's normally set to 0.5f, and that's a bit thicc).

    Kind Regards,

    Miggi124
     
  3. hsjaaa

    hsjaaa

    Joined:
    Apr 30, 2016
    Posts:
    29
    Thanks for reply.
    Read some paragraph about flocking behavior before, it seems works with a group of unit, some behavior like alignment, cohesion, separation works in a group, but if a single unit is selected and want to navigate through obstacles, teammates, enemies, feels like these behavior doesn't work.
    Let's say agent A's radius is 0.5f, if A's destination(set by player mouse click) is 0.6f away to where another agent B is standing, the A won't reach there without push agent B, this is hard in a crowd situation or B cant be pushed, if B happen to have destination close to A's, chances are they could both spinning around that position. Set stopping distance could help a bit about fighting position, but could lose some control response, also hard to stop pushing.
    Random offset in a crowd situation like a tense melee fight could result in failure or two many tries, so feel like the better solution is to find a position available and closer to player click point, or rearrange overlapping destination if both unit is moving to close destination.
     
    Last edited: May 1, 2019
  4. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    337
    Easiest way I've solved this is to set each AI's avoidancePriority to a random value (I usually reserve it between a certain range set for each group of AIs, say 0-9 for players, 10-19 for Friendlies, 20-70 for Baddies, etc...). While this isn't perfect, it can reduce the frequency of total lockups.
     
  5. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    337
    Another method might be to give each unit a set of points surrounding it. Perform a sphere check when nearing your destination and if a unit is found, grab whatever data structure it uses to store these nearby points, sphere check that, and recursively keep doing this until you find an empty spot to set your final destination. This might get costly if you are dealing with hundred of units though. However, it would at least give you a nice formation at all times.
     
  6. hsjaaa

    hsjaaa

    Joined:
    Apr 30, 2016
    Posts:
    29
    Sounds like a very good idea since my game won't deal with that much agents
    Ideally it's best if can decide when or who to push, but it seems Unity have no api for that. Random avoidance Priority seems can let the navigation avoidance more smoother, looks like a good workaround.
    Thanks!