- Home /
How can I improve this player death coroutine?
Hello,
I'm trying to use this coroutine to have my player a) freeze in place on death b) play the full death animation clip and c) load the level. If I put isKinematic before the yield, the animation doesn't finish. If I put it after, the player continues falling after the animation is played. Any suggestions would be greatly appreciated!
private IEnumerator DeathAnim ()
{
deathSound.Play ();//plays the death sound
animation.Play("Death");//plays the death animation
yield return new WaitForSeconds(.3f);//give the animation time to finish
rigidbody2D.isKinematic = true;//freeze the rigidbody2D of the player
Application.LoadLevel (Application.loadedLevel);//reload the current level
}
Answer by Danao · Nov 01, 2014 at 05:28 AM
This is the result of what worked well for me:
private IEnumerator DeathAnim ()
{
rigidbody2D.isKinematic = true;//freeze the rigidbody2D of the player
deathSound.Play ();//plays the death sound
animator.SetInteger ("AnimState", 1);
yield return new WaitForSeconds(1);//Use the length of the animation clip as the wait time for yield
Application.LoadLevel (Application.loadedLevel);//reload the current level
}
Answer by HarshadK · Oct 21, 2014 at 11:14 AM
You can use the length of the animation as the wait parameter to your yield statement.
private IEnumerator DeathAnim ()
{
deathSound.Play ();//plays the death sound
animation.Play("Death");//plays the death animation
yield return new WaitForSeconds(animation["Death"].length);//Use the length of the animation clip as the wait time for yield
rigidbody2D.isKinematic = true;//freeze the rigidbody2D of the player
Application.LoadLevel (Application.loadedLevel);//reload the current level
}
Thanks for the reply Harshad$$anonymous$$! I tried what you suggested above, but for some reason I can't get $$anonymous$$onoDevelop to recognize the .length part. Any ideas what I'm doing wrong?
Ok, it's recognized now, but telling me that there is no Animation attached to the player. I'm not sure how that is possible since the script is attached to the player with the animator...
I've tried both ways, getting the no Animation error with Animator and getting a Null Reference with Animation.
Your answer
Follow this Question
Related Questions
yield WaitForSeconds compatibility? 0 Answers
How To Create Coroutine Delegates? 1 Answer
www class blocks script execution 1 Answer
C# yield not doing anything 2 Answers
Yield Problems 2 Answers