Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to make sure the Nav agent has no path?

Discussion in 'Navigation' started by MinhocaNice, Jul 30, 2022.

  1. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    In my FPS game, there are (currently) certain places where enemies simply can't get to but the player can. While solving these is an objective in the future, I want to make sure enemies know they are never going to be able to get to these places instead of glitching out moving inch by inch in weird directions and rapidly alternating between pursuing the player and giving up doing so.

    How do I make sure of that? I tried the using
    NavMeshAgent.hasPath
    and
    NavMeshAgent.isStopped.isPathStale
    to determine if there is a path and
    NavMeshAgent.isStopped
    to stop them but that didn't work out very well.

    Code (CSharp):
    1.     void ChaseTarget (int TargetNum)
    2.     {
    3.         if (!TargetStats[TargetNum].Dead)
    4.         {
    5.             float DistanceFromPlayer = Vector3.Distance(Player [TargetNum].position, transform.position);
    6.             Agent.SetDestination(Player [TargetNum].position);
    7.  
    8.             if (Agent.hasPath && !Agent.isPathStale)
    9.             {
    10.                 Agent.isStopped = false;
    11.                 EnemyStatus = EnemyState.Pursuing;
    12.  
    13.                 if (DistanceFromPlayer < Enemy.AttackReach)
    14.                 {
    15.                     FaceTarget(TargetNum);
    16.                 }
    17.             }
    18.             else
    19.             {
    20.                 ForgetTarget();
    21.             }
    22.         }
    23.     }
    24.  
    25.     void ForgetTarget ()
    26.     {
    27.         Agent.isStopped = true;
    28.         CurrentPlayerTarget = -1;
    29.         EnemyStatus = EnemyState.Idle;
    30.     }
    31.  
     
  2. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Any ideas?
     
  3. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    296
    So my suggestion would be to cache the latest SetDestination vector to the player, and then if the path is complete you check the distance between those. If there is a big gap, you know they will not be able to reach them.

    To check if they have reached their closest point, you could try to work with NavMeshAgent.remainingDistance.
     
    MinhocaNice likes this.
  4. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    I already have scripts for the enemies to "forget" the player when they get too far, but I am not talking about distance, I am talking about places where the player is completely unreacheable (for example, if the player jumped to a high location that there is no path to connect).
     
  5. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    296
    Then I am not sure if I understand your question correctly.

    You know if they can reach the player if the endpoint of the enemy path has a distance to the player.