I'm working on functionality in Unity where if a game object's health is zero, it does and the object is destroyed. This works fine as is, I reduce the health to zero, the die animation plays and the game object is destroyed. I want to have the script wait a certain amount of time and then run the destroy method. I thought using WaitForSeconds() would do the trick, and technically, it does, but the death animation continues to play until the object is destroyed. My question is, how do I get the animation to stop playing as the script waits to destroy the object?
C#
void Die(){
GetComponent<Animation>().CrossFade(die.name);
//If the current animation time is greater than 90% of the entire animation length, destroy the object.
if(GetComponent<Animation>()[die.name].time > GetComponent<Animation>()[die.name].length * 0.9){
StartCoroutine(RemoveBody());
}
}
IEnumerator RemoveBody(){
yield return new WaitForSeconds(5);
Destroy(gameObject);
}