I currently have an Enemy character targeting my player, and playing a walk animation while moving, but when it stops, it does not transition to the idle, but continues to play the walk. Here is the code, any suggestions as to what I can be doing differently?
Thanks!
using UnityEngine;
using System.Collections;
public class EnemyAISphere : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
//max distance from target before attack
public int maxDistance;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 2;
}
// Update is called once per frame
void Update () {
//Draws Targeting Line for Debugging
Debug.DrawLine(target.position, myTransform.position, Color.green);
if(moveSpeed > 0)
animation.CrossFade("walk");
else
animation.CrossFade("idle");
//NPC Look atTarget
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//NPC will move is its distance from target is greater then the max distance. Sets NPC range
if(Vector3.Distance(target.position, myTransform.position) > maxDistance){
//NPC Move Towards Targeting
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}