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

[SOLVED] Repath if nav mesh is blocked by another nav agent

Discussion in 'Navigation' started by Brocan, Feb 1, 2016.

  1. Brocan

    Brocan

    Joined:
    Mar 22, 2012
    Posts:
    45
    First of all, sorry if this was already asked, but after a lot of search I didn't found a solution...

    I've a problem related to multiple nav agents. I've a nav mesh with small corridors, two nav agents can't cross in opposite directions because is too narrow. My problem is that if my agent A goes opposite to B in the same corridor, instead of check that they can't achieve the current path because is blocked by another nav agent, they stuck forever one in front another, the desired behaviour should be that one of them should select another path although it has a higher cost.

    I've checked also the avoidance priority, but with this the lower one is pushed in the higher one's direction until they are outside the corridor...

    Any ideas about how deal with this?

    Thanks in advance!
     
    Last edited: Feb 1, 2016
    elqtfy likes this.
  2. Brocan

    Brocan

    Joined:
    Mar 22, 2012
    Posts:
    45
    Ok, I've solved it by doing the following:

    - Each agent has a front detector (a trigger).
    - If two agents are in front one another (so trigger is activated), they check a priority.
    - If the priority is higher, you are able to continue your path and this agent is converted in an obstacle which carves the navmesh.
    - If your priority is lower, you need to recalculate your path, this agent path is reset and as the mesh has been carved by high priority agent, the current path becomes invalid and the lower priority agent calculates an alternative path.
    - After a random time, the higher priority agent becomes again an agent and continues its path.

    With this method the agents moves nicely.

    I hope that this solution can help for future problems like mine.
     
    Last edited: Feb 2, 2016
  3. pixxelbob

    pixxelbob

    Joined:
    Aug 26, 2014
    Posts:
    111
    Very interesting solution, thanks for sharing.
     
  4. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    this is a great solution , i just feel unity should handle this already , you'd think agents would path around each other , its kind of a no brainer , but still glad there is a work around .
     
  5. sentar

    sentar

    Joined:
    Feb 27, 2017
    Posts:
    44
    Hi Everyone, I would like to please add another solution to a similar problem for other people to find and try out.

    After reading more than a dozen articles I think I figured something out (somewhat to where it looks normal movement of people etc. etc.).

    My solution is not perfect as there is a little bit of "shaky" "pushy" type of movements on the AI side; however, it is far less and more realistic movement.

    I had a problem with 200+ AI Nav Mesh Agents do the "Jiggling"/ "Shaking" effects when they would all target one object. (in my test experiment scene). As they were all trying to force there way to that target.

    Things I tried:

    1.
    Setting Avoidance Priority; however, I soon found out that the priority level only goes to 99. so having double the AI, that couldn't help.

    2.
    I Tried setting the Nav Mesh Obstacle, and that made the shaking/jiggling/pushing of AI even worse.


    What I found that is working for me as a work around using C#:

    1.
    I made sure rigidbodies was on all my AI characters. (In my test I set continuous dynamic for collider detection)

    2.
    I made sure Sphere Colliders was on all my AI characters, and set trigger on.

    3.
    In my script I simply did this method (I apologize for my wording, and comments):

    // detect other gameObject tags that are not player, stop pushing other AI's away
    void OnTriggerEnter(Collider other)
    {
    if (other.tag != "Player") //tag not like player
    {

    this.GetComponent<NavMeshAgent>().isStopped = true; //since tag is not player stop this AI movement
    }
    }

    // opposite from enter detection, make AI move on path again
    void OnTriggerExit(Collider other)
    {
    if (other.tag != "Player") //the tag is not like player has left my trigger collider
    {

    this.GetComponent<NavMeshAgent>().isStopped = false; //make AI move again after trigger is over
    }
    }
     
    elqtfy likes this.
  6. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233
    I tried what you suggest but the navmesh agents still get stuck...
     
  7. RTF7

    RTF7

    Joined:
    Apr 30, 2020
    Posts:
    1
    Your solution is great mate! Thanks for sharing!