Search Unity

Question Animator: How to transition between 2 animations by code?

Discussion in 'Animation' started by FeastSC2, Oct 14, 2020.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I want to go from the hit to the stun animation by code.
    I can call Animator.Play("stun"); but then there won't be any smooth transition between the hit/stun animations.

    How can I create a custom transition I set up in script between the hit and stun animation?
    (must be by code and I should be able to affect the transition duration).

    EDIT: Ah, the answer is Animator.CrossFade(), cool!

    upload_2020-10-14_12-17-31.png
     
  2. acecase

    acecase

    Joined:
    Mar 16, 2014
    Posts:
    7
    I'm new to all of this, so I may be missing something, but the way I do what I think you're trying to do is with the animator using transisions and parameters.

    Add a boolean parameter like "damaged" and setup a transition from any states that you want to be able to transition from with the condition damaged == true. Then in code you get a reference to the animator
    Animator animator = GetComponent<Animator>();
    and just do
    animator.setBool("damaged", true);

    The animation will transition with whatever properties you setup in the animator, as well as transitioning out to the next animation.
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Hehe yes, but I wanted it do it by code in this case. The answer is Animator.CrossFade