So I want to know if there is a way to check if an object is longer reachable on the navmesh? The specific scenario is I have an enemy that chases the player and there are some spots on the navmesh where the player can hide (like a save room) where the enemy can no longer reach the player. The problem is that the enemy just gets confused when the player steps off the navmesh and doesn’t know what to do. If there a way to check if the player is no longer on the navmesh?
There are number of ways you can do that. Some I will mention below.
- You can create Tag to the safe room area game object so when player steps on it you can check collision and tag of the game object. If the tag is SafeRoom then player has entered in safe room, and you can change enemy behaviour.
- If safe room area is not a nav mesh area, then you can check if path is available or not for enemy to reach player, if path is not available then simply change enemy behaviour. Example code below.
NavMeshPath path = new(); //Create Nav mesh path object.
if (enemyAgent.CalculatePath(nextPos, path) &&
enemyAgent.pathStatus == NavMeshPathStatus.PathComplete) // Calculate path and check if path status is complete or not. If complete then enemy can reach player other wise there is no path available for enemy to reach player. enemyAgent is your NavMeshAgent object.
{
return true;
}
else
{
return false;
}
That worked right.