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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

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:
    974
    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:
    974
    Hehe yes, but I wanted it do it by code in this case. The answer is Animator.CrossFade