Input is Getting Queued?

posted to: Mecanim Scripting

Steps:

  1. Click the left-mouse button to enter rotation mode.
  2. Type the A-key. Cube keeps rotating, as expected.
  3. Type the W-key. Cube keeps rotating, as expected.
  4. Type the spacebar. Cube keeps rotating, as expected.
  5. Click the right-mouse button to exit rotation mode. It automatically goes into jumping before automatically proceeding to circling.
  6. Type the D-key to exit circling. It automatically goes into waving.
  7. Type the S-key to exit waving and go back to idle state.

The unexpected bits are when it automatically jumps to the next state, after exiting the current one. And there seems to be a priority as well (I'm guessing based on the order it's listed in the Update() function). For example, if you insert a left-mouse click after Step 5, it actually goes to rotating first, and only after right-mouse clicking to exit does it go to waving after.

Is the queueing expected behavior? Is there a way to turn that off?

  • Jonathan Gonzalez(jgonzalez) replied

    I believe it does get queued up as I've experienced this before as well. I was working on a fighting game and trying to create fast kicking and punches but often times after I've stopped touching inputs the animations would be "catching up". 

    I'm not sure if there's an easy way to stop that through the animation system, but through coding I would perform a check to see if one animation is currently playing before registering another input check. Here is the documentation on that: https://docs.unity3d.com/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html 

    In short, this is what you'd need:

     if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Jump"))

    m_Animator.GetCurrentAnimatorStateInfo(0) refers to the Animator layer 0, which is the default first layer. From there you specifically look for a state called "Jump" and determine if it's playing. This is either true or false, so a boolean check.

    So to expand on that, if I wanted to avoid double jumping because of animation "catch up" as mentioned above, I could check to see if that animation is currently playing, and if so then I won't trigger that animation to play. So this is pseudo code, but this is how I would approach it:

    bool isJumping;
    void Update ()
    {
    isJumping = if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Jump"));
    if(Input.GetButtonDown("Jump") && !isJumping)
    m_Animator.SetTrigger("Jumping");
    }
    

    I just stored the state info within a bool variable to make it easier to reference in code. So here I check to see if I press the Jump input (spacebar) and I also check to see if isJumping is false (! in front means false). If both of those are true, then I play the animation using a trigger parameter called Jumping.

    I'm glad you asked this question as it's something that has been bugging me a while but completely forgot about it. Definitely something I'll include for most fast paced animation gameplay in the future.