Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animator - When is the animator animation finished?

Discussion in 'Scripting' started by Zexanima, Sep 11, 2014.

  1. Zexanima

    Zexanima

    Joined:
    Jan 2, 2014
    Posts:
    33
    Here is my situation and I don't understand the animator class well enough to solve it. I have the animation "Walk" and the animation "Damage". I want it to crossfade to the damage animation when damaged, then once that animation is finished, go back to the walk animation. My main issue is I cant seem to find a way to tell when the current animation the animator is playing has concluded so I can go back to my walk animation.
     
  2. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    You have to use the Animator parameters inside the animator controller. Make a trigger, Damage inside the parameter field. Then when you are hit, set that flag to true. Have it go from your "Any State" to the damaged state.

    ex
    Code (csharp):
    1. void Hit(){
    2. animator.SetTrigger("Hit");
    3. }
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Why don't you just add a transition (white arrow in the Animator view) from Damage to Walk? When Damage is done, it will automatically transition back to Walk, no code necessary.
     
  4. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    You could do this too. The only issue is, if you loop it back to walk after damage and you are standing still it may play a frame from the walk cycle which could be fine guess.
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Good point. In this case, add two transitions: Damage-->Walk and Damage-->StandingStill. That is, unless Walk is actually a blend tree on a speed parameter that includes standing still, walking, and running. Then you only need one transition to the blend tree.