1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Play animation sequence

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

  1. kagura

    kagura

    Joined:
    Jan 3, 2011
    Posts:
    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
    Posts:
    1,363
    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
    Posts:
    54
    It is not work. Because normalize time is checked before animation complete.
     
  4. cemC

    cemC

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

    kagura

    Joined:
    Jan 3, 2011
    Posts:
    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
    Posts:
    54
    Please help me
     
  7. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,363
    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
    Posts:
    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
    Posts:
    1,363
    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
    Posts:
    67