LoginSignup
0

More than 5 years have passed since last update.

Unity: Apply an different animation with Animator

Posted at

Animator

When you have a bunch of animations (or a few of them), using the Unity's Animator works well. This tiny tip give you how to apply a different animation based on the scale of the attached gameobject.

Screen Shot 2016-02-05 at 13.49.04.png

Each object needs to have an Animator component.

Screen Shot 2016-02-05 at 13.49.26.png

Screenshots

The following images might help to grasp how each inspector works.
Animators need to have at least one parameter to decide what kind of transition is going to be triggered.

Screen Shot 2016-02-05 at 13.50.01.png

Screen Shot 2016-02-05 at 13.50.16.png

Screen Shot 2016-02-05 at 13.50.25.png

Screen Shot 2016-02-05 at 13.58.30.png

Screen Shot 2016-02-05 at 13.50.36.png

Screen Shot 2016-02-05 at 13.51.03.png

using UnityEngine;
using System.Collections;

public class CubeAnimBehaviour : StateMachineBehaviour {
        private float xScale;

    // OnStateEnter is called before OnStateEnter is called on any state inside this state machine
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        // first, need to check the target scale. based on this, create transition
        xScale = animator.transform.localScale.x;
        // then, set the next animation
        if (xScale == 1.0) {
                animator.SetInteger ("Animation Mode", 0);
        } else if (xScale == 2.0) {
                animator.SetInteger ("Animation Mode", 1);
        } else if (xScale == 3.0) {
                animator.SetInteger ("Animation Mode", 2);
        }
    }

    // OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateExit is called before OnStateExit is called on any state inside this state machine
    //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateMove is called before OnStateMove is called on any state inside this state machine
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateIK is called before OnStateIK is called on any state inside this state machine
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateMachineEnter is called when entering a statemachine via its Entry Node
    //override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash){
    //
    //}

    // OnStateMachineExit is called when exiting a statemachine via its Exit Node
    //override public void OnStateMachineExit(Animator animator, int stateMachinePathHash) {
    //
    //}
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0