How to use interruptions in Unity Animator to the fullest





The wow effect in games can be achieved not only with unexpected mechanics or plotters, but also with animation - in mobile projects, the latter often has a special emphasis. Therefore, I decided to share an interesting translation of material on Unity Animator. Inside - about the priority of transitions of animation and competent switching between states.



The interruption mechanism in Unity Animator is almost not worth the tutorials and documentation, so many simply stop studying it. Only with experience did I understand what patterns and signs there are to use an interrupt.



He divided the article into three parts:









Part 1: what is an “interrupt”?



Interruption is a feature of Unity Animator that allows you to stop the transition of animation from one state to another in order to move to the third.



This is the only way that allows you to perform a blend from an incomplete transition to the desired state. With it, you do not rely on a sharp change in state or, even worse, on Any State transitions. I’ll explain in more detail later.



There are several basic types of interruption:







Current State



If several transitions are provided from a certain state and one of them starts another, switching will be performed from the current point, as shown in the diagram:







Next State



Next State works similarly to Current State. But here, Animator will analyze transitions from the next state, while the transition from the current state is still in force.







Current State Then Next State (First, current state, then next)



Somewhere around here, people are bored by the theory, and they lose interest in the topic. Wait! This will take your skill with Animator to a new level.



Having mastered Current State and Next State, you will definitely understand the third type, because it is their combination. In it, Animator will analyze the current transition frame by frame in search of others that can be performed from the initial state. If they are not found, Animator will analyze the next state in search of a suitable transition. At the same time, switching to a new transition in the process of executing the previous one, Animator will behave in exactly the same way as in the examples above.



Next State Then Current State (Next state, then current)



Everything is like in the third type, only the order of analysis will be reverse.



Ordered Interruption







For each state, the priority of transitions (from top to bottom) is set, like this:







If before you did not know why to drag components in this list - voila! Here you rank the transition priority.



When the Ordered Interruption is checked, the Animator will only interrupt if the priority of the new transition is higher than the priority of the transition currently in progress. And the higher the transition is located in this list, the greater its priority.



Part 2: how to determine where to break



When working with interrupts, it is most difficult to determine exactly when to use them. And in some situations they are necessary - over time you begin to notice it.



Consider the most striking example. The familiar pattern?







It all comes down to Any State. Right?



No.



This is the first sign that most of your transitions are best done with interrupts. Transitions from Any State have the right to life in special cases, but most people use them when they really need (well, you guessed it) interruptions.



Current State Interrupt Patterns:







Often, Any State is created only for the transition to the second state, when the first has already begun. Forget it. Use interrupts.



Another example: we have a transition from FirstState to SecondState, but Exit Time is not set, the transition time is 0 and a condition is specified. Interruption will ideally solve this problem. You cannot turn to Any State, since it has already been used too often, and by adding it to SecondState, you activate states that are not needed in this case.



Next State Interrupt Patterns:







Quite often there are transitions with completely insane Exit Time and transition conditions. Set the interrupt correctly, and you will more easily achieve the same goal. As you can see, Any State is often found where interrupts are needed.



Remember that you can add multiple transitions from two identical states in order to successfully use the interrupt.



Usually, all other patterns are combinations of the examples described above.



Typical situations for interruption:



In some situations, interrupts are the best way out. A little practice at Animator, and you will easily learn to notice them.



a) The next action should immediately trigger an animation.



This often happens in action games where there are many inputs and you need to quickly take the following actions.



For example, people often use Any State transitions (or multiple transitions with different configurations) to smoothly transition from a double jump to a jerk.



It is necessary to map all possible actions, set the duration of events and add it to states and transitions. And then set interrupts to correctly switch from one event to another.



b) The animation has started, but the situation has changed, and we need to move on to a different sequence of animations.



This often happens with UI Animators, in which you can skip some menu items, and submenus can be displayed on top of everything else.



People often add multiple Animators to the same object, and set conditions for turning them on / off to skip one animation and start another. This is another example where interrupts are worth using.



c) You need to exit the current animation chain.



Interrupts can be connected with each other - you get a sequence of interruptions that will lead you to a new chain. In this case, all states will be correctly and timely completed. You can use Next State interrupts here.



Part 3: everything in Animator should be clear



Working with Animator is a special kind of art. The more practice, the more obvious the progress. A clear and consistent workflow always has a positive effect on results.



Recommendations to avoid confusion in Animator:



  1. Use Sub-State Machines and try to circumvent transitions to internal states. The correct Sub-State Machine independently works on input and output, uses transitions that direct the flow inward, and knows what to do with the result. This way you can avoid critical issues when working in Animator.
  2. Use Any State as little as possible. Think about whether this is really necessary. Contact your colleagues. And only if everyone agrees, use the Any State transitions.
  3. If it seems to you that your Animator should look easier than now - put it in order. This is the best way to hone your skills.



All Articles