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

NavMesh Agent not stopping when is near the player

Discussion in 'Navigation' started by yosefstudios, Oct 6, 2020.

  1. yosefstudios

    yosefstudios

    Joined:
    May 8, 2015
    Posts:
    129
    Basically the title. I have on my scene a navmesh agent with some functionality via scripting. The script checks the distance between the agent (enemy) and the player, and if the distance is less or equal to 8, the enemy is going to chase the player. When the distance is less or equal to 2, the enemy is going to attack the player.

    The "chasing radar" works fine, however, the agent doesn't stop neither attack the player when he meets the distance value given before.
    This is my code at the moment:
    Code (CSharp):
    1. DistanceToPoint = Vector3.Distance(transform.position, target.position);
    2.         //Debug.Log("Current distance " + Convert.ToInt32(DistanceToPoint));
    3.  
    4.         if (currentHealth > 0f) //While its still alive
    5.         {
    6.             if (DistanceToPoint <= 8f) //If the player is far from this, the enemy is not going to move
    7.             {
    8.                 ChasePlayer();
    9.  
    10.                 if (DistanceToPoint <= 1f) //When the distance between this and the enemy is at least 2, the enemy is going to attack
    11.                 {
    12.                     navAgent.isStopped = true;
    13.                     Attack();
    14.                 }
    15.             }
    16.             else {
    17.                 Idle();
    18.             }
    19.         }
    I also tried this to see if it was any difference:
    Code (CSharp):
    1. if (currentHealth > 0f) //Mientras que la vida sea mayor a cero
    2.         {
    3.             if (DistanceToPoint <= 8f)
    4.             {
    5.                 ChasePlayer();
    6.             }
    7.             else {
    8.                 Idle();
    9.             }
    10.  
    11.             if (DistanceToPoint <= 1f)
    12.             {
    13.                 navAgent.isStopped = true;
    14.                 Attack();
    15.             }
    16.         }
    And nothing.

    Not even modifying the Stopping distance parameter on inspector changes something. The agent just gets stuck walking and colliding with the player.

    Am I missing something? maybe overriding something I'm not aware of?

    I'm working on Unity 5.6