Play animation sequence

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

  1. kagura

    kagura

    New Member

    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

    Member

    Joined:
    Jul 3, 2009
    Messages:
    1,218
    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

    New Member

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

    cemC

    Member

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

    kagura

    New Member

    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

    New Member

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

    AnomalusUndrdog

    Member

    Joined:
    Jul 3, 2009
    Messages:
    1,218
    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

    New Member

    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

    Member

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