- Home /
NavMesh agent bug
I have an enemy and my character. Between them there is a wall. and the enemy should go around the wall to get to the character (using navMesh.setDestination() function), But he doesnt. he tries to go through the wall.
If I use any other empty object as the target of the navMesh the enemy goes around the wall. I tried changing the y position of the gameObject to see if its about the y but it didnt matter. Any help?? Thank you!
Here is the script:
public float patrolSpeed = 2f; // The nav mesh agent's speed when patrolling.
public float chaseSpeed = 5f; // The nav mesh agent's speed when chasing.
public float chaseWaitTime = 5f; // The amount of time to wait when the last sighting is reached.
public float patrolWaitTime = 1f; // The amount of time to wait when the patrol way point is reached.
public Transform[] patrolWayPoints; // An array of transforms for the patrol route.
private EnemyAnimation enemySight; // Reference to the EnemySight script.
private NavMeshAgent nav; // Reference to the nav mesh agent.
private Transform player; // Reference to the player's transform.
private float chaseTimer; // A timer for the chaseWaitTime.
private float patrolTimer; // A timer for the patrolWaitTime.
private int wayPointIndex; // A counter for the way point array.
void Awake ()
{
// Setting up the references.
enemySight = GetComponent<EnemyAnimation>();
nav = GetComponent<NavMeshAgent>();
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update ()
{
if(enemySight.InSight)
Chasing();
else
// ... patrol.
Patrolling();
}
void Chasing ()
{
nav.SetDestination( player.transform.position) ;
}
void Patrolling ()
{
// Set an appropriate speed for the NavMeshAgent.
nav.speed = patrolSpeed;
// If near the next waypoint or there is no destination...
if(!enemySight.InSight )
{
// ... increment the timer.
patrolTimer += Time.deltaTime;
// If the timer exceeds the wait time...
if(patrolTimer >= patrolWaitTime)
{
// ... increment the wayPointIndex.
if(wayPointIndex == patrolWayPoints.Length - 1)
wayPointIndex = 0;
else
wayPointIndex++;
// Reset the timer.
patrolTimer = 0;
}
}
else
// If not near a destination, reset the timer.
patrolTimer = 0;
// Set the destination to the patrolWayPoint.
nav.destination = patrolWayPoints[wayPointIndex].position;
}
When you change the player to an empty game object, does the position, rotation, and scale stay the same? The player and the new gameObject should both be dynamic. They should not have NavmeshObstacle components and they should not be marked as static. If you are noticing that the NavAgent is still following the wrong path after checking that the above are true, I suggest filing a bug report. NavAgents should only traverse Walkable areas of the Navmesh, so unless the Wall is not creating a blocking path, there is a bug.