Hello!
How can I make for example a death animation play out, and make the code pause until the animation has ended?
I want to play out the animation, and afterwards teleport it back to the spawn.
Here’s my code:
void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "Enemy")
Die();
}
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Goal")
{
GameManager.CompleteLevel();
}
}
void Die()
{
//animation.Play ("PlayerDeath");
Instantiate(deathParticles, transform.position, Quaternion.Euler (270,0,0));
transform.position = spawn;
rigidbody.Sleep();
rigidbody.AddForce (0, -5, 0);
}
}
Thank you in advance!
I don’t know about the rest of your code, but the animation can return the amount of time the animation clip is. You can start a coroutine with that time for that named animation(PlayerDeath);
void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "Enemy")
StartCoroutine(Die());
}
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Goal")
{
GameManager.CompleteLevel();
}
}
IEnumerator Die()
{
animation.Play ("PlayerDeath");
// optionally by name
// float animationLength = animation["PlayerDeath"].clip.length
// yield return new WaitForSeconds(animationLength);
yield return new WaitForSeconds(animation.clip.length);
Instantiate(deathParticles, transform.position, Quaternion.Euler (270,0,0));
transform.position = spawn;
rigidbody.Sleep();
rigidbody.AddForce (0, -5, 0);
}
This was covered in the Scripting API under AnimationClip.length
StartCoroutine Scripting API