Search Unity

Nav mesh avoidance

Discussion in 'Navigation' started by thomkoled, Mar 2, 2015.

  1. thomkoled

    thomkoled

    Joined:
    Oct 28, 2014
    Posts:
    11
    Hi all, I have a problem with AI. I am thinking about it for a long time.. but I cant figure it out.. So, I am using Unity nav mesh pathfinding for my mobs/enemies. The avoidance between mobs works quite good. But if there are many mobs, they are trying to find the shortest path to the player. Few mobs reached the player, but other mobs are stucked. They should find another way, around other mobs..

    This is my actual behavior:


    How mobs should go to the player:


    Does someone have any idea, how to fix it? Thanks for your help!
     
  2. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Perhaps, instead of having them move right towards the player, you could have the agents move to positions slightly off to the sides first, and then have them move from there to the player.
     
  3. thomkoled

    thomkoled

    Joined:
    Oct 28, 2014
    Posts:
    11
    Hi,
    I am using this method:

    I am using navmesh pathfinding for player, and enemies. (agents).. if enemies are chasing player, there is navmesh agent enabled. (and navmesh obstacle disabled). And if are mobs attacking, there is navmesh obstacle enabled. (agent disabled).. They are not active in the same time/frame!

    But, i want to use carving.. Navmesh obstacle -> carve. After enabling the navmesh obstacle (and disabling agent) my player starts to move to the "spawn" position.. (and other agents too).. I can not explain this behavior, why are they moving...

    Without carve feature, all agents stay in own position. Could someone help me please?
     
  4. tomlou

    tomlou

    Joined:
    Mar 19, 2015
    Posts:
    4
    watching for more reply
     
  5. thomkoled

    thomkoled

    Joined:
    Oct 28, 2014
    Posts:
    11
    Hi, I just disabled all navmeshagents, when the player/mob does not move... But I have a new problem.. again :D
    When the agent starts to move (chasing the player for example), there are moments when the agent ist dragged back. You can see it in the video below. I don't know, if I am wrong, or it is a weird bug.

     
  6. scplayer1

    scplayer1

    Joined:
    Feb 4, 2016
    Posts:
    11
    I am having this problem as well. Anyone heard of a solution?
     
  7. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,362
    If you want the mob to surround the player, don't use carving, instead give each mob a different destination around the player.
     
  8. baalor

    baalor

    Joined:
    Jun 19, 2015
    Posts:
    2
    I use this in turn-based game now.
    The sequence is that:
    1.disable carve
    2. wait for NavMesh.SamplePosition on your position returning true

    while(!NavMesh.SamplePosition(GetCurrPlayer().transform.position, out hit, 0.2f, NavMesh.AllAreas))
    {
    yield return new WaitForEndOfFrame();
    }
    3. Then enable navmeshAgent and start your move!

    NavMesh need some time to recalculate itself when you enable/disable carving.
    When you put your navMeshAgent enabled and don't have navmesh under agent (becouse it is not recalculated) it moves to the nearest navmesh.

    Whole code looks like that:

    Code (CSharp):
    1.  
    2. GetCurrPlayer().transform.FindChild("NavMeshObs").GetComponent<NavMeshObstacle>().enabled = false;
    3. GameManager.instance.StartCoroutine(lateNavMeshAgentEnable());
    4.  
    5. ...
    6.  
    7.  
    8. static IEnumerator lateNavMeshAgentEnable()
    9.     {
    10.         NavMeshHit hit;
    11.         while(!NavMesh.SamplePosition(GetCurrPlayer().transform.position, out hit, 0.2f, NavMesh.AllAreas))
    12.         {
    13.             yield return new WaitForEndOfFrame();
    14.         }
    15.         GetCurrPlayer().GetComponent<NavMeshAgent>().enabled = true;
    16.  
    17.         GetCurrPlayer().TurnStart();
    18.     }
    19.  
    Maybe it will be too long for some FPS, but it is OK for TBS game.

    The other thing i'm mesing with right now, is that SOMETIMES navMesh is broken after recalculating with that carving AI and i have breaked plane in too picies and players can go through an "invisible navMesh wall" in the middle of the room. Don't have any decision about it.