Search Unity

Question How to approach this problem with Navmesh Agents local avoidance, asset suggestion maybe.

Discussion in 'Navigation' started by Tomasz_Pasterski, Dec 19, 2020.

  1. Tomasz_Pasterski

    Tomasz_Pasterski

    Joined:
    Aug 12, 2014
    Posts:
    99
    Hello,
    Wanna ask if someone can point me out some approach or suggest an asset which can help resolve my problem with navmesh agents local avoidance in RTS project.

    I encountered a problem when i have around 50 agents and order them attack move to some destination which means they should move to the destination and attack any enemy they encounter along the way. When they encounter any enemy they should stop at fire range and shoot enemy till destroying him then continue to destination. Simple move attack order from RTS games.
    While i select half of my units, around 25 of them, and order them attack move intentionaly behind enemy position to trigger them attacking those enemies along the way they stop at/around fire range and attack. Then i select the rest of my units and give them same order to similiar destination but those got on the way first half of my units already stopped at firing range and shooting at enemy. I expect the second group to bypass those already attacking and move to attack destination but ofcourse they should encounter same enemies to attack, stop and shoot but at different angle but those second group try to push those already stopped so they can continue in straight line. I do some weird behaviour with trigger collider behind every of my units which when they are stoped and shoot under attack move order and other unit try to bash from behind those with trigger triggered move forward a bit till that unit behind enter that fire range and stop to shoot but thats awful looking and hardly match tank behaviour.

    Entire situation is on this video, that button at the bottom i click to move them is not Attack as tooltip says but it works as attack/move order, forgot to change tooltip:



    Any suggestion for assets with helpful system or any approach? I prefer to use something using navmesh system cause i have many other behaviours and systems coded using it alreadym, thanks in advance.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    An imperfect solution is boids.

    Basically, you do not allow NavAgents to DIRECTLY control your units and instead read where the nav agent wants to go.

    The actual movement of the object would be composed out of sum of multiple vectors:

    * Where nav agent wants to go.
    * Avoidance vector (sum of all vectors facing AWAY from nearby friendly units).

    Sum those up, and use resulting vector to direct object movement.

    That would mean you'd need to maintain list of nearby units for every unit, though.

    upload_2020-12-21_2-28-58.png
    In this scenario, Unit A wnats to move in direction provided by Navigation.
    Unit C is far away, and is ignored. Unit B is within Avoidance Radius, therefore Unit A calculates avoidance vector which is how far you'd need to adjust position to move away form Unit B so it will be no longer within avoidance radius.

    Combined together those will result in roughly this kind of movement (Gray line):
    upload_2020-12-21_2-31-36.png
    Unit A will try to circle unit B, will hit Unit C with avoidance radius and will squeeze between them while pushing them away. (because those two also try to avoid other units)

    This is an imperfect solution, however. If you make two groups operate this way and order them to pass through each other, they will do it, but the movement will not be perfectly efficient.

    Does this explanation make sense to you?
     
  3. Tomasz_Pasterski

    Tomasz_Pasterski

    Joined:
    Aug 12, 2014
    Posts:
    99
    Thx for answer and suggestion.
    Yeah i get it, after work i will work on this solution and see the results but you say its imperfect so what can be perfect solution for you?
    Maybe you know some asset which can handle this, in general, RTS movement with proper local avoidance cause i dig in asset store and cant find anything except A* but want to avoid it cause my project relly heavy on Navmesh and i prefer to stick to it.
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    I can't recommend one, because I usually don't buy assets, and haven't worked on RTS games.

    I do recall mentions of a Flow Pathfinding, or Flow Field Pathfinding, maybe that one would be more suitable. I'd expect there to be assets for it on the store.

    -------

    For the sake of completeness, Classic boids algorithms is used to simulate flock of "birds", and has more rules.

    https://en.wikipedia.org/wiki/Boids

    It is just that I found that this approach with "avoidance vector" to be incredibly useful and it can be very easily adapted to many situations.
     
    Tomasz_Pasterski likes this.
  5. Tomasz_Pasterski

    Tomasz_Pasterski

    Joined:
    Aug 12, 2014
    Posts:
    99

    I today actualy start to do something with your suggestion but started to have another problem with how i convert sum of all those vectors into path? Agent have only "calculatePath: which calculate path and store it to specified by me destination or "setPath" for setting path from path variable i already got calculated by "calculatePath" i suppose.
    Or you mean for that "avoidance vector" i must take away contol from agent and use transform??
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    You don't convert them into a path.

    You tell the agent where your actor is, and the agent tells you where it wants the actor to go.

    Then you use vector provided by the agent together with avoidance vectors. And move in direction of resulting sum.

    You take away control from the agent, and move characters yourself. The agent doesn't know anything about other units, avoidance vectors and so on. It only looks up a path on the map towards the destination.
     
    Tomasz_Pasterski likes this.
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Another approach could be to use a "flow field" algorithm. A search will get you a fair few results.
     
  8. nico_st_29

    nico_st_29

    Joined:
    Mar 14, 2020
    Posts:
    69
    Hey Tomasz,

    I had a similar issue with Unity's NavMesh on my RTS game. Take a look at this article, this is what you need to do:
    https://gamedev.net/articles/progra...avoidance-for-rpgrts-games-using-unity-r3703/

    1) On the units already stopped, you turn on NavMesh Obstacle / turn off NavMesh Agent
    2) The new units coming up only have NavMesh Agent active and they will find a way around those units.
    3) You can also have a look at the avoidancePredictionTime field (https://docs.unity3d.com/ScriptReference/AI.NavMesh-avoidancePredictionTime.html)

    Then you might get some other problems that I'm currently dealing with and I'd be happy to have a look together at those (namely when there's lots of units using NavMesh with SetDestination the NavMesh starts taking too long to calc and your units get 'stuck' temporarily which is really annoying).
     
    MahaloAloha likes this.
  9. NixyNick

    NixyNick

    Joined:
    Oct 11, 2020
    Posts:
    12
    You said you are asking for assets so you might want to check out the Ultimate AI System - it is an asset
     
    Tomasz_Pasterski likes this.