Key press recognized during Mecanim transitions?

I have a 2-stage melee attack logic:

  1. on pressing Fire1 the character arms
    the hit, and should maintain the
    arming while Fire1 is pressed
  2. on releasing Fire1 the character releases the blow and does damage to
    enemies, or if Fire2 is pressed
    arming is cancelled and we return to
    IDLE

In mecanim, I have the following setup (with scripting logic included):

  • IDLE state → Input.GetButton(“Fire1”) → ARMED State
  • ARMED State → Input.GetButtonUp(“Fire1”) - > HITTING State
  • OR
  • ARMED State → Input.GetButtonDown(“Fire2”) → cancel arming and return to IDLE
    State
  • HITTING State → exit time → IDLE State

The issue is that if Fire1 gets released during the transition from IDLE to ARMED, the whole process breaks down. We are now in the ARMED state (since we did press Fire1 to trigger the transition to it) but we can’t get to Fire1 UP without pressing it down again. Moreover, we get stuck in the ARMED state while Fire1 is NOT pressed as it should be.

What am I missing?

1 Answer

1

In your code to handle the Input.GetButtonUp(“Fire1”), you need to check that the current state is ARMED and that the animator is not in transition, which you can do using Animator.GetCurrentAnimatorStateInfo(0).IsName(“ARMED”) and !Animator.IsInTransition(0) respectively (assuming these states are all in your base animation layer)

Just combine the two previous methods with Animator. GetNextAnimatorStateInfo: http://docs.unity3d.com/Documentation//ScriptReference/Animator.GetNextAnimatorStateInfo.html

Wow, this might be it. Thanks, I'll check it out and get back to you. :)

[I converted your answer to a comment]. And this is all taking place in a secondary animation layer, not in the base layer? (which would be GetCurrentAnimatorStateInfo(0) etc. ? You might need to post a picture of your Animator window, and please show the set of conditions in the transitions from IDLE -> ARMED and back again

OK, I eventually figured it out, after a lot of trial and error, including identifying particular transitions by name. All I had to do was to change the HITTING condition, from if(Input.GetButtonUp("Fire1")) { anim.SetBool("Hit", true); } else { anim.SetBool("Hit", false); } to if(!Input.GetButton("Fire1")) { anim.SetBool("Hit", true); } else { anim.SetBool("Hit", false); } God damn it :(