- Home /
Damage Animation doesn't play
I want to play animation damage when taking shot, but it doesn't play, just stop for a moment. This is my code
function OnTriggerEnter(other : Collider){
if(other.tag == "shot"){
move = false;
animation.Play("damage01");
health--;
Destroy(other);
}
}
function Update(){
if(move && health > 0){
GetComponent(NavMeshAgent).destination = target.position;
animation.Play("walk01");
}
playerDistance = Vector3.Distance(player.transform.position, transform.position);
if(playerDistance <= 1){
if(!attacking && health > 0){
move = false;
this.GetComponent(NavMeshAgent).Stop();
animation.Play("attack01");
Invoke("ApplyDamage", 1);
attacking = true;
}
}
if(playerDistance > 1 && !attacking && health > 0){
move = true;
animation.Play("walk01");
}
if(health <= 0){
die();
}
}
When asking questions, please detail what is happening versus what you intend to have happen.
To be fair, both are equally important in reshaping existing behavior into desired behavior.
Answer by Gardosen · Oct 30, 2014 at 09:25 AM
On your OnTriggerEnter Method you are writing
animation.Play("damage01");
health--;
Destroy(other);
is the animation played on the other object? if so, you have to wait till the animation is over, and then destroy it. otherwise he will start the animation but goes further in progress.
i also would recommend you to place the Destroy(other); call into a separate method like
public void DestroyObject(GameObject _gameObject){
Destroy(_gameObject);
}
and then make a EvenTrigger call in your animation to this method.
this way you can synchronize the destroying of the object with a certain timeframe of your animation.
Greetings Gardosen