Animator Controller
Last updated
Last updated
The Unity Animator controller provides a visual programming language implementation of a state machine to control coordination of animations which can be attached to gameObjects.
Drag Multiple Sprites into Scene Panel: To create an animation, we can select a series of sprites and drag them into the scene window. We are then prompted to save this animation. This immediately creates several components:
Animation clip:
Animator controller:
Animator component
In addition, in order to integrate animations with our Unity program, we will probably want to use the following additional items:
Animator parameters
Custom C# controller script
GameObject with RigidBody2D - used for GameObjects that will be moved by the Physics System.
GameObject with Collider2D - to detect collisions with other GamObjects to: earn points, lose health, get collectable objects.
In 2D mode, we can create animation clips using sprites. We can use a series of sprites, where each sprite is in a slightly different position, so that when we show these sprites in a sequential loop, we see animated behavior. Or, we can use a single sprite, and use the Animation editor to modify some feature of the sprite over time using the dope-sheet or curves editor.
The Animator Controller, Mechanim, is a visual interface for creating a state machine to control animations in Unity. It has the ability to utilize input parameters as event signals, these input parameters provide the connection between the Mechanim state-machine and custom C# scripts. The animation controller has a set of nodes that correspond to states, it is easy to add arc - transitions between nodes. Each transition can be configured to use any number of conditions to determine when a transition is executed to change the current state of the system. There are also many online tutorial examples that show how to use Mechanim as a state-machine to control game-logic, instead of animations.
In order to integrate an animation with a gameObject, we need a 2D gameObject that has a Sprite Renderer, then we can add an Animator Component from the component miscellaneous tab. Then select the empty animator-controller section in the inspector to select an animation controller from your assets folder.
To control a animation associated with a gameObject, outside of the Animator Controller, such as user-input, then we'll need a custom C# script that can manage the logic to determine what input events have occurred and whether the input should impact the animation controller state. Our custom script will send input signals to the Animator Controller State Machine, where the animator controller state machine will consider the current animator state, the input signals, and the defined transition conditions to determine whether to switch to a new animation clip.
Below is a simple example class: PlayerController.cs which checks for left-right arrow keyboard input, or the jump key which maps to the spacebar on a MAC.
In Fixed-Update, the code checks for input, then uses if-else statement blocks to determine what value to send to the Animator component that is attached to the gameObject. Below is the link to a set of free sprites including the cat sprites used in the starter code example animations.
Link to Free - 2D GameArt - Cat Sprite Assets
Unity 2D Character Animation Tutorial - John Stejskal Recommended for creating simple animations 2D Animation in Unity - Raywenderlich Animate UI Elements rm2kdev: Unity Animator Tutorial with Player Sprite Sheet Sprite Sheet Animation Tutorial