Search Unity

I can't get CalculatePath to work [SOLVED]

Discussion in 'Scripting' started by MonkeyPuzzle, May 31, 2020.

  1. MonkeyPuzzle

    MonkeyPuzzle

    Joined:
    Jan 17, 2016
    Posts:
    119
    I can't get CalculatePath to work. The enemy has no problem finding the player if it is on the nav grid, but if the player is off the nav grid, the enemy gets as close as possible then stops.

    I want it to go back to patrol, if it can't reach the player, or play some taunt animation, but this script always returns "Not Blocked." Any help is appreciated.

    The enemy has the navmesh agent and the player navmesh obstacle components

    Code (CSharp):
    1.  
    2. NavMeshPath path = new NavMeshPath();
    3. agent.destination = playerTarget.transform.position;
    4. agent.CalculatePath(agent.destination, path);
    5.  
    6.  if (path.status == NavMeshPathStatus.PathPartial)
    7.      {
    8.           Debug.Log("Blocked");
    9.       }
    10.       else
    11.       {
    12.           Debug.Log("Not Blocked");
    13.        }
    14.  
     
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    when a path becomes partial the destination is automatically changed to the closest point that the NavMeshAgent can still reach.

    Maybe set a bool to check if player when off the grid ?
     
    Last edited: May 31, 2020
  3. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    And maybe add a check see if destination is the player if is not then go back patrol
     
  4. MonkeyPuzzle

    MonkeyPuzzle

    Joined:
    Jan 17, 2016
    Posts:
    119
    Checking if the destination changed did not work, however checking if the player was off the navigation mesh did. Thanks for steering me in the right direction!

    This thread has a robust solution for this. https://forum.unity.com/threads/solved-navmeshpath-status-always-returning-complete.486243/

    My character flies, so I will probably need to cast a ray to the ground for that instance.
     
    Yanne065 likes this.
  5. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Or you could have a trigger if player exit the trigger he's off the grid