Overview
Character movements like walking, jumping, and attacking are essential for breathing life into games. Unity provides a powerful system for creating and managing these 2D animations relatively easily.
Unity's 2D animation system consists of three main components:
- Animation window: Where you arrange sprites on a timeline to create Animation Clips—the "film strips" of your animations.
- Animator component: Attached to game objects to control which animations play.
- Animator Controller: A state machine that defines transition rules between animation clips (e.g., from "Idle" to "Walk").
This article explains the basic workflow: creating animation clips from sprite sheets and switching playback based on character state using Animator Controllers.
Step 1: Creating Animation Clips
First, create animation clips for each character action (idle, walk, jump, etc.).
-
Preparation: Place the game object you want to animate (with a
Sprite Renderercomponent) in the scene. Assume your sprites are already sliced from a sprite sheet. -
Open Animation window: With the object selected, open the Animation window via
Window > Animation > Animation. -
Create first clip: The window displays "To begin animating [ObjectName], create an Animator and an Animation Clip." Click
Create. This automatically creates:- Animator Controller: An asset managing animation states (
.controllerfile). - Animation Clip: Your first clip (
.animfile). Save it with a name like "Idle". - The
Animatorcomponent is attached to your object with the created controller assigned.
- Animator Controller: An asset managing animation states (
-
Register sprites: Select all sliced sprites for the idle animation from the Project window and drag them onto the Animation window timeline. Keyframes are automatically created, registering sprites in sequence.
-
Preview: Press the play button in the Animation window to preview in the Scene view. If too fast, drag keyframes on the timeline to adjust timing.
-
Create other clips: Similarly, click the clip name (e.g., "Idle") in the Animation window, select
Create New Clip..., and create clips for "Walk," "Jump," etc., registering corresponding sprites.
Step 2: Managing States with Animator Controller
Once animation clips are ready, use the Animator Controller to define when each animation plays.
-
Open Animator window: Double-click the Animator Controller asset in Project window, or select
Window > Animation > Animator. -
Check states: The window shows "states" (colored boxes) corresponding to your animation clips. The state with an arrow from
Entryis the default animation (usually "Idle"). -
Create parameters: Create parameters to control animation state from scripts. Open the
Parameterstab at the top-left of Animator window and add parameters with the+button:Float: Continuous values like speed.Int: Integer values like weapon type.Bool: True/false states like "is jumping."Trigger: One-time events like attacks or taking damage.
For example, create a
FloatcalledSpeedand aBoolcalledIsJumping. -
Create transitions: Create "arrows" (transitions) to switch animations between states.
- Right-click "Idle," select
Make Transition, and drag the arrow to "Walk." This creates an Idle → Walk transition. - Create the reverse Walk → Idle transition similarly.
- Right-click "Idle," select
-
Set transition conditions: Select a transition (arrow) and set conditions in Inspector:
- Idle → Walk: In
Conditions, setSpeedGreater0.1. - Walk → Idle: In
Conditions, setSpeedLess0.1. - Uncheck
Has Exit Time. When enabled, transitions wait for the current animation to finish, causing sluggish responsiveness.
- Idle → Walk: In
Step 3: Controlling Animator from Script
Finally, update Animator parameters from script based on player input and state.
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class PlayerAnimationController : MonoBehaviour
{
private Animator animator;
private Rigidbody2D rb;
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>(); // Need Rigidbody2D to get velocity
}
void Update()
{
// Get absolute horizontal velocity and set to Animator's "Speed" parameter
float speed = Mathf.Abs(rb.velocity.x);
animator.SetFloat("Speed", speed);
// Example: Set "IsJumping" to true when jump button is pressed
if (Input.GetButtonDown("Jump"))
{
animator.SetBool("IsJumping", true);
}
}
}
Using methods like animator.SetFloat(), animator.SetBool(), and animator.SetTrigger() to update parameters, the Animator Controller automatically evaluates conditions and transitions to appropriate animation states.
Summary
Unity's 2D animation system may seem complex at first, but it's very systematic once you understand each component's role.
- Animation window: Arrange sprites to create Animation Clips (
.anim). - Animator Controller (
.controller): Define transitions between clips and their conditions. - Create parameters (Float, Bool, etc.) as transition conditions.
- Get the Animator component reference in script and control animations with
SetFloatandSetBool.
Master this workflow to bring your characters' actions to life.