【Unity】Unity 2D Animation: Bringing Characters to Life with Animator

Created: 2025-12-07

2D animations bring characters to life. Learn how to create animation clips using the Animation window and control playback with Animator Controllers.

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:

  1. Animation window: Where you arrange sprites on a timeline to create Animation Clips—the "film strips" of your animations.
  2. Animator component: Attached to game objects to control which animations play.
  3. 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.).

  1. Preparation: Place the game object you want to animate (with a Sprite Renderer component) in the scene. Assume your sprites are already sliced from a sprite sheet.

  2. Open Animation window: With the object selected, open the Animation window via Window > Animation > Animation.

  3. 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 (.controller file).
    • Animation Clip: Your first clip (.anim file). Save it with a name like "Idle".
    • The Animator component is attached to your object with the created controller assigned.
  4. 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.

  5. 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.

  6. 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.

  1. Open Animator window: Double-click the Animator Controller asset in Project window, or select Window > Animation > Animator.

  2. Check states: The window shows "states" (colored boxes) corresponding to your animation clips. The state with an arrow from Entry is the default animation (usually "Idle").

  3. Create parameters: Create parameters to control animation state from scripts. Open the Parameters tab 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 Float called Speed and a Bool called IsJumping.

  4. 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.
  5. Set transition conditions: Select a transition (arrow) and set conditions in Inspector:

    • Idle → Walk: In Conditions, set Speed Greater 0.1.
    • Walk → Idle: In Conditions, set Speed Less 0.1.
    • Uncheck Has Exit Time. When enabled, transitions wait for the current animation to finish, causing sluggish responsiveness.

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 SetFloat and SetBool.

Master this workflow to bring your characters' actions to life.