Switch an animation from any state – Unity

When creating an action game or a battle game, you may want to link a key input to an animation. All animation need transition from anywhere. If each state were connected with all others, Animator should become hard to see. In such case you can use “Any State” of Animator.

Environment: Unity 2018.4.6f

 

Any State

A state linked to “Any State” can transition from all states.

But if it’s just linked by “make transition”, same animation is called many times. It need uncheck “Can Transition To Self“. This parameter is on a cursor of transition.

 

Call an animation at a key input

  The state isn’t called many times but at the end of animation a model just stand.  To chage it to a default state you may link each state to “Exit”, then the state is infinitely called again.

If it want to change the “Stay” animation, a parameter of Animator need to be initialized.   You change the parameter to “Stay” value after switching a state.

using UnityEngine;
using System.Collections; //Need this for coroutine.

public class AnimationManager : MonoBehaviour{
  //Set a model's "Animation Controller" on Inspecter.
  [SerializeField] Animator animator; 

 //Call this method at an inputing key.
 public void PlayAnimation(int state) {
   //Create "Command" in Parameters and change each animation by its value.
     animator.SetInteger("Command", state);
   StartCoroutine(InitializeParameter());
    }

 IEnumerator InitializeParameter(){ 
   //Need a flame for switching animation.
    //Without this the above method can't switch a state.
    yield return null;

   animator.SetInteger("Command", 0);
    }
}

 

Stop coroutine when an input is continuous

If a project is simple action game, the above code have nothing problem. However, about an intense typing like battle game this code does not work well. If it’s invoked continuously, a follow input is overwrote by the  previous one’s coroutine. So, it need the process for stopping the coroutine or a input.
using UnityEngine;
using System.Collections;

public class AnimationManager : MonoBehaviour{
  [SerializeField] Animator animator; 
  bool isContinuous;
 //Call this method at an inputing key.
 public void PlayAnimation(int state) {
      if(isContinuous){
      //The way to overwrite the input by new one.
      StopCoroutine("InitializeParameter");
      animator.SetInteger("Command", 0);

      //The way to stop input.
      //return;
      }
     
     isContinuous = true;
     animator.SetInteger("Command", state);
     //Need to call coroutine by string for stopping this.
     //Or the coroutine add IEnumerator variable and call the process by it.
     //The method way(like XXX()) creates new coroutine every time it's invoked.
   StartCoroutine("InitializeParameter");
    }

 IEnumerator InitializeParameter(){ 
   //Need a flame for switching animation.
    //Without this the above method can't switch a state.
    yield return null;

   animator.SetInteger("Command", 0);
     //Switch the flag after this process has finished.
     isCoutinuous = false;
    }
}

*Haw to stop coroutine.

References