Animation chaining with Mecanim based on input

I have a scenario where I want to be able to chain certain animations together based upon user input. So for example if they were to do X + X + Y that would do a punch_1 animation followed by punch_2 animation followed by kick_1 animation.

The problem I have is that when the animation starts playing I am not entirely sure when to trigger the next update, so the workflow would be:

  • User Presses X
  • Animation transitions from current animation to the relevant attack one
  • User Presses X
  • Animation transitions from current animation to the relevant attack one
  • User does nothing
  • Animation reverts to idle

I am using playmaker but that is not too important as the underlying logic can be expressed via an FSM or raw coded behaviour.

So in the above I would basically listen out for an attack button being pressed then would get the animation component, and call animator.play(attackStateName, layer, normalizedTime); then at this point I get a bit unsure as to what to do as I need to basically provide a timeout for the user to press or not press a button then transition over. However I am not sure how to wait for this as I basically need to know when the animation window should start and when it should end, so should this just be based upon some time value? or should it be a check like animatorInfo.IsName(attackStateName) then action the attack. However if I was in idle and then punched, although the key has been pressed it will still be in the idle animation for a given period before it transitioned to the attack one, so its not quite as simple as animations dont really start and end they just transition. So you are always working on the basis that you are half-way through an animation, never at the start or end. This also raises the problem of when do you want a transition to occur, as if you were going from punch_1 to punch_2 you would not want to transition at the moment the user presses a button, you want the first animation to finish then do the next one.

As I am sure you can see here I am quite new to this but basically want to know ways to solve this common sort of problem? I don’t need a playmaker specific answer, as I am happy to transpose whatever code/psudo code would be needed to custom actions wherever needed (Although would rather use pre-made playmaker actions wherever possible).

I would generally use trigger parameters to control these transitions in the Animator window - you can either let the animator’s state machine define whether the chaining succeeded, or perform your own checks in code before setting the triggers to initiate the transitions.

If you need to pass data back from the animator to your code (e.g. a window of opportunity for button presses) then you can do that using parameters too, if you have Pro - I think this isn’t available in Basic though.

If you’re using Basic then you can fall back to reading the elapsed time for the current clip and hard-coding some values; you can also add a bool/float/etc field to a component on the animated object and use the dopesheet and inspector to animate the field. Then you can read that field from other scripts to get timing information back from the animation.