1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

Play animation sequence

Discussion in 'Editor & General Support' started by kagura, Feb 19, 2011.

  1. kagura

    kagura

    Joined:
    Jan 3, 2011
    Messages:
    54
    Hi,
    I have 3 animation A,B,C.
    I want to play animation A then B then C. When animation C is completed, I will show a notice.
    How can I do that? I don't know how to callback when animation complete .

    Thanks so much.
     
  2. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Messages:
    1,307
    Use CrossFadeQueued() so the animations will play one after the other. To check if an animation has finished playing, store the AnimationState in a temporary variable:

    Note: I am using C#

    Add this as a member variable to your class:
    Code (csharp):
    Put this in Start() (change nameOfAnimation to the actual animation name):
    Code (csharp):
    1. anim = animation["nameOfAnimation"];
    Then put this in Update():
    Code (csharp):
    1.  
    2. if (anim.normalizedTime == 1.0f)
    3. {
    4.   // animation is finished. do something here
    5. }
    6.  
    I have not tested this code so tell me if there's anything wrong.
     
  3. kagura

    kagura

    Joined:
    Jan 3, 2011
    Messages:
    54
    It is not work. Because normalize time is checked before animation complete.
     
  4. cemC

    cemC

    Joined:
    Dec 23, 2010
    Messages:
    214
    You can use animation.Crossfade( ) function.
     
  5. kagura

    kagura

    Joined:
    Jan 3, 2011
    Messages:
    54
    Code (csharp):
    1. void Update()
    2. {
    3.      animation.CrossFade("attack");
    4.    
    5.      if (anim.normalizedTime == 1.0f)
    6.      {
    7.           //  do something..
    8.      }
    9.  
    10. }
    The code in do something is not work
     
  6. kagura

    kagura

    Joined:
    Jan 3, 2011
    Messages:
    54
    Please help me
     
  7. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Messages:
    1,307
    Again, I said use CrossFadeQueued, not CrossFade, so that they will be played one after the other.

    Code (csharp):
    1.  
    2.  
    3. void Start()
    4. {
    5.   animation.CrossFadeQueued("animation1");
    6.   animation.CrossFadeQueued("animation2");
    7.   animation.CrossFadeQueued("animation3");
    8.   anim = animation["animation3"];
    9. }
    10.  
    11. void Update()
    12. {
    13.   if (anim.normalizedTime == 1.0f)
    14.   {
    15.     Debug.Log("animation3 is finished");
    16.   }
    17. }
    18.  
    nomalizedTime starts at 0.0 at the beginning, 0.5 at the middle of the animation, and will become 1.0 when the animation is finished.
     
  8. kagura

    kagura

    Joined:
    Jan 3, 2011
    Messages:
    54
    Thanks.
    I want to play animation in a function , not on Start()
    Example when I click a button, animaton sequence start, then show a notice when complete
     
  9. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Messages:
    1,307
    Then put the code that plays the animation in a different function then call that function when you need it to.
     
  10. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Messages:
    61