- Home /
How can I check if an animation has finished playing?
I am working on a jump --> fall --> land system. I want to determine when my landing animation has finished so that I can set my "landing" boolean to false. Here is the simple code:
// If the character is landing
else if (landing) {
Debug.Log("Landing");
animation[landingAnimation.name].wrapMode = WrapMode.Once;
if(animation.clip.name!=landingAnimation.name || !animation.IsPlaying(landingAnimation.name) )
animation.CrossFade(landingAnimation.name, 0.1f);
else
landing = false;
//falling = false;
Debug.Log("Landing is false");
}
As you can see, the landing animation is set to play once. I have commented out the boolean because otherwise it fires before the animation finishes. I have tried using !animation.isPlaying but it wasn't working. Is there another way? Or maybe I haven't set up the isPlaying correctly.
Answer by flamy · Feb 11, 2012 at 02:14 PM
you are doing it right but seems like it is the problem of crossfading continuously inside the if loop, meaning the animation stays for ever!! change the code a bit it will be fine
/ If the character is landing
else if (landing) {
Debug.Log("Landing");
animation[landingAnimation.name].wrapMode = WrapMode.Once;
if(animation.clip.name!=landingAnimation.name
|| !animation.IsPlaying(landingAnimation.name) )
animation.CrossFade(landingAnimation.name, 0.1f);
else
landing = false;
}
I hope this would work and also please bare me, if it has some typo, i couldnot test the code rite now!!
Thank you for your response flamy. I have updated the script in the answer. It is playing the animation now but once the landing boolean is set to false, it seems to go back and loop through the entire "else if" statement again continuously. I've made the falling and landing booleans public so I can watch them in the inspector, and the debug logs of "Landing"... "Landing is false"... "Landing"... seem to prove that the last statement keeps looping. Would you know why this is happening?
hmm it seems the variable landing is being changed from outside this block to true. Check where it turns true and you would get it