Hello, i have an enemy character that has a death animation. When i kill the enemy, the death animation begins to play (In the "Death" function), stops halfway and repeats function Update and begins the chase animation again. How do i fix this?
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var attackThreshold = 3; // distance within which to attack
var chaseThreshold = 10; // distance within which to start chasing
var giveUpThreshold = 20; // distance beyond which AI gives up
var attackRepeatTime = 1; // delay between attacks when within range
var maximumHitPoints = 5.0;
var hitPoints = 5.0;
private var chasing = false;
private var attackTime : float;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
// check distance to target every frame:
var distance = (target.position - myTransform.position).magnitude;
if (chasing) {
Debug.Log ("player is close");
animation.CrossFade("Walk");
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
// give up, if too far away from target:
if (distance > giveUpThreshold) {
chasing = false;
}
// attack, if close enough, and if time is OK:
if (distance < attackThreshold && Time.time > attackTime) {
animation.CrossFade("Attack");
target.SendMessage( "ApplyDamage", 1);
attackTime = Time.time + attackRepeatTime;
}
} else {
// not currently chasing.
Debug.Log("not close yet " + distance);
animation.CrossFade("Idle");
// start chasing if target comes close enough
if (distance < chaseThreshold) {
chasing = true;
}
}
}
function OnCollisionEnter (collision : Collision) {
if(collision.gameObject.name==("SmallProjectile1(Clone)"))
{
var contact : ContactPoint = collision.contacts[0];
var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
Hit();
}
}
function Hit () {
// Apply damage
hitPoints -= 1;
if (hitPoints < 0.0) {
Die();
}
}
function Die(){
animation.Play("Death");
chasing = false;
}