x


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.

more ▼

asked Feb 11 '12 at 01:25 PM

Pilot gravatar image

Pilot
372 11 13 19

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!!

more ▼

answered Feb 11 '12 at 02:14 PM

flamy gravatar image

flamy
3.5k 4 11 37

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?

Feb 11 '12 at 03:25 PM Pilot

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

Feb 11 '12 at 04:10 PM flamy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3767
x93
x31
x3

asked: Feb 11 '12 at 01:25 PM

Seen: 2961 times

Last Updated: Feb 11 '12 at 04:10 PM