I try to play die animation when the enemy health points reaches less than 10, but the problem is the animation die clip is keep playing and not stop, here is the code:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
Transform target;
int rotationSpeed;
int moveSpeed;
float followDistance;
bool canMove;
bool isDead = false;
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
rotationSpeed = 28;
moveSpeed = 5;
followDistance = 5f;
canMove = false;
animation["die"].wrapMode = WrapMode.Once;
}
// Update is called once per frame
void Update () {
EnemyMove ();
}
void EnemyMove()
{
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation (target.position - transform.position),
rotationSpeed );
EnemyHealth eh =(EnemyHealth) gameObject.GetComponent("EnemyHealth");
if(canMove){
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
if(eh.curHealth >= 10)
{
if(Vector3.Distance(transform.position,target.position) < followDistance){
canMove = false;
animation.CrossFade ("attack");
}
else
{
canMove = true;
animation.Play ("run");
}
}
else
{
animation.Play("die");
}
}
}