Search Unity

Question Bug where fast Nav Mesh Agent gets "stuck" on walls

Discussion in 'Navigation' started by ryanlafauci00, Jan 8, 2022.

  1. ryanlafauci00

    ryanlafauci00

    Joined:
    Nov 6, 2021
    Posts:
    4
    As a part of my game, when the enemy does not see the player, it navigates randomly on the Navmesh. This works fine until it reaches a point where it would have to make a 90 degree turn into a hallway or in/out of a room. I've captured what the issue looks like here (ignore the constant rotations of the enemy, it is set to constantly look at the player). The path for the agent is to move from that smaller room into that larger room to the right.

    Now, the enemy will eventually be able to make it out of this, but this can take quite a bit of time. Time in which the player can navigate through the game's goals without having to deal with the enemy. This bug can also be used to the player's advantage for an easy escape from the enemy. Finally, this bug seems to occur at a more severe extent the faster the agent is set to move, and not at all if the agent is slow enough.

    As for the variables that the agent had at the time of this, you can click here to see a capture of what those were set to.

    Finally, here are my code snippets for this simple random navigation system. Of course, these snippets are in a script attached to the enemy.

    Code (CSharp):
    1. nav.speed = GlobalVars.gameDifficulty * (waypointSpeedMultiplier - waypointSpeedMultiplierSubtraction);
    2. nav.acceleration = (GlobalVars.gameDifficulty * 1.3f) * (waypointSpeedMultiplier - waypointSpeedMultiplierSubtraction);
    3. if ((!nav.pathPending && !nav.hasPath) || nav.remainingDistance < 15)
    4. {
    5.       nav.SetDestination(RandomNavmeshLocation(100f));
    6. }
    Code (CSharp):
    1.     private Vector3 RandomNavmeshLocation(float radius)
    2.     {
    3.         Vector3 randomDirection = Random.insideUnitSphere * radius;
    4.         randomDirection += transform.position;
    5.         NavMeshHit hit;
    6.         Vector3 finalPosition = Vector3.zero;
    7.         if (NavMesh.SamplePosition(randomDirection, out hit, radius, 1))
    8.         {
    9.             float dist = Vector3.Distance(hit.position, this.transform.position);
    10.             if (dist < 15f)
    11.             {
    12.                 return RandomNavmeshLocation(radius);
    13.             }
    14.             else finalPosition = hit.position;
    15.         }
    16.         return finalPosition;
    17.     }
    This is the only bug I've had with this so far but it is quite annoying. Once this issue is fixed, the enemy navigation will be flawless. Any ideas will help greatly, thanks ahead of time.
     
  2. ryanlafauci00

    ryanlafauci00

    Joined:
    Nov 6, 2021
    Posts:
    4
    Sorry, forgot to post the image of my variables in the OP.

     
  3. Leigh966

    Leigh966

    Joined:
    Mar 11, 2020
    Posts:
    2
    Hey, I know I'm very late to the party but I saw your post and had the exact same issue. One thing I noticed I had in common with you: a very high angular speed. I noticed the lower I brought mine down the better it got at not getting stuck. About 120 is working for me in a maze full of 90 degree turns