Trigger to reverse current animation state

So, I have a “Door opening” animation. I want trigger A to activate “open door” animation with speed of 1.0f.

When trigger B occurs, it should reverse the same exact “open door” animation with speed of -2.0f.

The player can then run trigger A (even before “open door” animation reaches the beginning) to redo the whole sequence.

I tried to basically have one state with initial speed multiplier of 0 where I can write animator.setFloat("speed", 1.0); for opening and setting -2.0f for reversing. The problem with it was that the speed multiplier was shooting through and going below 0, so when the user wanted to open the door again, it would have to wait to get the speed multiplier back to 0, and then moving with the animation.

How can I do such a simple thing?

Alright, so here’s how I solved it. I don’t think this is the “Unity-intended” way but I think my method is clean enough.

The screenshot shows an:

  • “Idle” state where nothing happens
  • “Open” state where basically the default door opening animation occurs

Second screenshot shows a speed parameter that controls the speed multiplier. This will basically reverse the door opening animation when animator.setFloat("speed", -1.0f) is set.

Now for the transitions:

  • Idle -> Open transition will trigger when speed parameter is > 0.0f. It has Has Exit Time turned off.
  • Open -> Idle transition will trigger when speed parameter is < 0.0f. It has Has Exit Time turned off.

This piece of code handles setting the speed parameter:

public void toggleAnim(bool b)
    if (b)
        animator.SetFloat("speed", 1.0f);
        animator.SetFloat("speed", -2.0f);

Basically, the flow goes as follows: Idle state is the default state. When the speed parameter is set to 1.0f, the animation would transition to Open state. This can be interrupted at anytime during the animation. When the user “fails” to open the door, I’ll set speed parameter to -1.0f or anything negative to reverse AND break out of the Open state back to Idle.