I’ve followed a Unity video available on YouTube to create a state machine for your AI, but however, when the enemy is patrolling, they walk through walls. I’ve tried to add the RigidBody component onto the enemy, but this completely bugs out their movement going to each waypoint.
Here is the code that makes the AI move to each waypoint
private void Look()
{
RaycastHit hit;
if (Physics.Raycast(enemy.eyes.transform.position, enemy.eyes.transform.forward, out hit, enemy.sightRange) && hit.collider.CompareTag("Player"))
{
enemy.chaseTarget = hit.transform;
InChaseState();
}
}
void Patrol()
{
enemy.meshRendererFlag.material.color = Color.green; //Test to see what state the enemy is in
enemy.navMeshAgent.destination = enemy.waypoints[nextWaypoint].position; //Select the current waypoint
enemy.navMeshAgent.Resume(); //Start walking again
if (enemy.navMeshAgent.remainingDistance <= enemy.navMeshAgent.stoppingDistance && !enemy.navMeshAgent.pathPending) //Checking to see if we've reached our destination
{
nextWaypoint = (nextWaypoint + 1) % enemy.waypoints.Length; //Go to next waypoint and should loop as expected
}
}
Anyone know how to edit this code to make the enemy walk around walls in the map to reach each waypoint? Thanks.