When creating an activeness game or a battle game, you may want to link a fundamental input to an animation. All animation demand transition from anywhere. If each state were connected with all others, Animator should become hard to meet. In such example you can employ "Any State" of Animator.

Environment: Unity 2018.4.6f

  • How to employ Animator "Any State".
  • The way for simple action game.
  • Take measure out for continuous inputs as gainsay game.

Any State

A state linked to "Whatever State" tin can transition from all states.

But if it'south just linked by "make transition", same blitheness is called many times. It need uncheck "Can Transition To Self". This parameter is on a cursor of transition.

Call an blitheness at a cardinal input

  The land isn't chosen many times but at the stop of animation a model just stand.  To chage it to a default state you may link each state to "Exit", and so the country is infinitely chosen once more.

If it want to modify the "Stay" animation, a parameter of Animator need to be initialized.  You modify the parameter to "Stay" value after switching a land.

                  using UnityEngine; using Organization.Collections; //Need this for coroutine.  public class AnimationManager : MonoBehaviour{   //Set up 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 modify each animation past its value.      animator.SetInteger("Control", state);    StartCoroutine(InitializeParameter());     }   IEnumerator InitializeParameter(){     //Need a flame for switching animation.     //Without this the in a higher place method tin can't switch a state.     yield return null;     animator.SetInteger("Control", 0);     } }                

Stop coroutine when an input is continuous

If a projection is simple action game, the to a higher place code have nothing problem. However, about an intense typing like battle game this code does not work well. If it'southward invoked continuously, a follow input is overwrote by the  previous one's coroutine. Then, it demand 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 central.  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 end 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 tin can't switch a state.     yield render goose egg;     animator.SetInteger("Command", 0);      //Switch the flag after this process has finished.      isCoutinuous = false;     } }                

*Haw to end coroutine.

References