The interruption mechanism in Unity Animator tool has not a lot of tutorials, documentation or clear explanation on it, and that leads people to skip learning about them.
As I worked more and more with animator during my years as a game developer, I started to get very used to how the interruption works and found out there are some patterns we find in animators that indicates that an interruption should be in place, replacing the current transitions.
This article will be separated in three areas.
The first one will explain what are interruptions, what kind of interruptions we can find, and what are their best use cases.
The second part will talk about the patterns that indicate that an interruption source needs to be placed, so we can easily look into our animators and see how we can improve them by replacing redundant transitions with interruptions.
The third part will talk about organizing your animator as a whole, preventing the uncontrolled and unorganized growth of an animator, especially when working on big teams where multiple people contributes to the same animator.
Part 1: What the heck is an interruption?
An interruption is an option that any transition on Unity’s animator tool have that allows an animation to blend from the middle of the current transition, to the state that interrupted that transition.
This is important to know because it’s the only way we can get good blending from in-between transitions, instead of relying on abrupt changes of states to reach the target state for the given situation, or even worse…”ANY STATE” transitions! More on that latter.
We have three main types of interrupts:
Current state interrupts mean that if a state has multiple transitions departing from it, if another transition would happen while the transition is taking place, a blend will start from the point of the current transition, to the transition that will lead to the other state, as shown in the image below:
As you might have guessed, next state works similarly to current state, except animator now will analyze the next state transitions as soon as the the current one starts, listening to transitions that might be satisfied in the next state while the current state is taking place.
Current State Then Next State:
This is where people starts to get lost, or stops caring...but DON'T! This is what will bring your animator game to the next level!
It's not hard to understand this one after understanding the previous two! It's a combination of both, except Animator will analyze frame by frame during the current transition, transitions that might have been satisfied on the departing state, and if none is found, it will then analyze the next state in search of a satisfied transition, and will behave exactly like the two previously shown once a transition is satisfied during the current transition.
Next State Then Current State:
If we got this far, this is as easy as it gets. Just like the previous case, except the order of analysis is switched!
Bonus! Ordered Interruption
Every state has a list of transitions ordered from top to bottom, as shown in the image below:
If you never understood why this is a draggable list, now you do! It's how you order what transitions might take precedence and interrupt others.
When you check the "Ordered Interruption" checkbox, what you are doing is letting Animator know that it should only interrupt if the transition that was satisfied is above the current one in that list.
The closer to the top the transition, the more priority it has.
Part 2: How to spot interruption opportunities
The aspect I see most people struggling with when it comes to interruptions, is correctly identifying their use cases.
Most people understand the concept, but once they start producing an Animators, they never use it.
We are about to change that! There are some clear patterns we can spot right away that can be exchanged by an interruption, and the more you do this little exercise, the easier it gets to start thinking in interruption!
First of all, lets talk about the biggest teller that you are in desperate need of interruptions.
Does this image feels familiiar?
"Anystate" it all!! Right?
This is the first clear sign that most of your transitions can be better done with interruptions, since "any state" transition is truly needed on very specific scenarios, and most people use it when what they actually wanted to do is...you guessed it, interruptions!
Current State Interruption Patterns:
If you have an "any state" transition that is only there to jump from the first transition to that state in case the first one started already...well, you can switch all of those right now! Interruptions are exactly for that case!
Another very clear one is when we add a transition from the "FirstState" to "SecondState" without exit time and transition time equals to 0, and a condition.
When we do transition like that, what you really wanted was an interrupt, but you couldn't afford an "any state" as you probably used it so much, that it's now triggering states you don't want if you add it to "SecondState".
Next State Interruption Patters:
It's very common to see that type of transitions with all sort of crazy configuration on exit times and transition conditions to achieve what can easily be done by a well placed interruption!
As you can see, "AnyState" also is commonly seen when we are in need of interruptions.
Remember that you can add multiple transitions from two same states in order to make use of a good interruption.
All remaining patterns are usually combination of those seen above
Common Interruption Situations:
There are some very common situations where we can spot an interruption right away, and just knowing about them may help you spot them as you are building the animator!
My next action shall start the animation right away
This is common on action games, where you have multiple inputs and can have fast subsequent actions.
What is commonly observed is that people resort to "Any State" transitions or multiple transitions with different configurations to achieve a smooth transition between a double jump followed by a dash, for example.
But what must be done, is mapping all the possible actions and making a clear timeline of events, add that timeline in states and transitions, and start ordering the interrupts in a way that this timeline can be skipped correctly.
My animation started, but the situation changed and needs to follow another path of animations.
This is common on UI animators, where menus can be skipped, or sub-menus can pop up in front of the current one.
It's common to see people adding multiple animators on the same game object, and having them be turned on/off so they can skip the current animation and place the new one instead.
This is another place where interruptions shine bright!
I need to cancel out of the current animation path.
Interruptions can be chained together, and have a sequence of interruptions that takes you out all the way to the new path, allowing that all states are fired correctly in the expected and planned timeline.Next state interrupts can be of use!
Keeping My Animator Tidy and Clean
Creating animators is an art on itself! The more you create them, the better you get, the more you achieve with a clean and cohesive flow!
I follow some good practices that keeps me from messing an animator too much.
- Use sub state machines, and avoid transitions to inner states. Planning for a sub state machine that can work itself in and out, and having transitions that router the flow inside the sub state machine and handles the outcome will prevent your animator complexity from getting out of hand.
- Avoid "Any State" as much as possible. Think hard if you really need it. If you think you still do, research more, and think more. If it's still the best option, ask for a second opinion. If it's all agreed on, THEN you use "Any State" transitions. They are very tempting as they are
- If you think your animator should be simpler than it is, put in the extra work to work on it until you feel it's how a good animator should look like. This is the best way to hone your animator creation skills.
Using interruptions is a matter of practice! Get your old animators and give it a go!