Hi
I’m trying to create a AI enemy by using Nav Mesh Agent and animation controller.
When I setup basic AI and Nav Mesh Agent enemy follows very well. But I don’t know how to properly add animation. I have a Idle and walk animation. Idle works good. The problem is that when I add a walk animation enemy can’t turn.
This is a C# code for AI:
public class Enemy_Pursuit : MonoBehaviour
{
public LayerMask detectionLayer;
private Animator anim;
private Transform myTransform;
private NavMeshAgent myNavMeshAgent;
private Collider[] hitColliders;
private float checkRate;
private float nextCheck;
private float detectionRadius = 5;
// Use this for initialization
void Start()
{
SetInitialReferences();
}
// Update is called once per frame
void Update()
{
CheckIfPlayerInRange();
}
void SetInitialReferences()
{
myTransform = transform;
anim = GetComponent<Animator>();
myNavMeshAgent = GetComponent<NavMeshAgent>();
checkRate = 0.7f;
}
void CheckIfPlayerInRange()
{
if((Time.time > nextCheck) && (myNavMeshAgent.enabled == true))
{
nextCheck = Time.time + checkRate;
hitColliders = Physics.OverlapSphere(myTransform.position, detectionRadius, detectionLayer);
if(hitColliders.Length > 0)
{
myNavMeshAgent.SetDestination(hitColliders[0].transform.position);
}
}
}
}