AI >> NavMesh Surface
add the navigation surface to our game
remember to click the bake button
after bake it look like this
on this button to see the bake surface
add the enemy animator to control the enemy animation
add the animator to the enemy
the animator look like this
add trigger parameter and rename it as isIdle
the condition choose isIdle
using UnityEngine;
using UnityEngine.AI;
public class enemyAI : MonoBehaviour
{
private NavMeshAgent agent;
private GameObject player;
private Animator animator;
public float distanceEnemy = 5f;
float distance2Chase = 7f;
bool isWalkStatus = false;
// Update is called once per frame
void Update()
{
player = GameObject.FindWithTag("Player");
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
//Debug.Log(player.transform.position);
float distance = Vector3.Distance(transform.position, player.transform.position);
if (distance < distance2Chase)
{
if (distance > distanceEnemy)
{
agent.isStopped = false;
agent.SetDestination(player.transform.position);
}
else
{
agent.isStopped = true;
}
}
else
{
// Outside chase range
agent.isStopped = true;
animator.SetTrigger("isIdle");
}
}
}