Skip to main content

๐ŸŽž๏ธ Lesson 11.1: Frame-Based Sprite Animation

The oldest trick in animation still runs most 2D games: flip through a sequence of drawings fast enough and the eye sees motion. In Unity, those drawings are sprite frames, and you turn them into a playable clip right inside the Animation window. Best of all, the clips plug straight into the same Animator state machine you built back in Module 1 โ€” the 2D and 3D animation systems are one and the same.

๐ŸŽฏ Learning Objectives

By the end of this lesson, you will be able to:

  • Create an Animation Clip by dragging a sequence of sprite frames into the Animation window
  • Read the Sprite track and its keyframes, and understand the Sample Rate (frames per second)
  • Control whether a clip loops and adjust playback speed without re-authoring frames
  • Hook clips into an Animator Controller with the same states and transitions from Module 1
  • Drive the 2D state machine from code with the new Input System via Speed and Grounded

Estimated Time: 40 minutes  ยท  Prerequisite: Lesson 10.2 (Sprite Sheets & Slicing) and Module 1 (the Animator & state machines)

In This Lesson

Animation Is a Flipbook

Think of the little flipbook you drew in the corner of a school notebook: a stack of pages, each slightly different, riffled fast enough to look alive. Frame-based 2D animation is exactly that. You have a row of sprite images โ€” say four poses of a character running โ€” and Unity shows them one after another, several times a second, so the character appears to run.

In Lesson 10.2 you sliced a sprite sheet into individual sprites. Those slices are your flipbook pages. Now you'll arrange them on a timeline so they play in order.

๐Ÿ“– Definition

Animation Clip: a Unity asset (.anim) that records how properties change over time. For frame-based 2D that "property" is the Sprite shown by a Sprite Renderer โ€” the clip simply swaps which sprite is displayed at each keyframe.

Because a clip animates any property, the same Animation window you use here also animates position, colour, or scale. But for classic 2D characters, the star property is the sprite swap.

Creating a Clip from Frames

The fastest way to build a sprite clip is to let Unity do the work โ€” drag the frames in and it creates the clip and the keyframes for you:

  1. Select your character GameObject in the scene (it should already have a Sprite Renderer).
  2. Open Window โ–ธ Animation โ–ธ Animation (shortcut Ctrl+6). With nothing recorded yet, the window shows a Create button.
  3. In the Project window, expand your sliced sprite sheet and select the run frames in order (click the first, Shift-click the last).
  4. Drag all selected frames onto the Animation window's timeline. Unity asks where to save the new .anim file, then drops each sprite onto the Sprite track as evenly spaced keyframes.

That's it โ€” press the Play button inside the Animation window and your character runs. Unity also auto-creates an Animator Controller and an Animator component on the object the first time you record a clip, exactly like it did in Module 1.

โœ… Pro Tip

Name frames so they sort correctly: run_0, run_1, โ€ฆ run_10 will mis-order (10 sorts before 2). Use zero-padding โ€” run_00, run_01 โ€” so the drag brings them in the right sequence every time.

Reading the Animation Window

The Animation window is a timeline editor. Along the top runs a ruler marked in frames and seconds; down the left is the list of animated properties; the little diamonds on each row are keyframes. For a sprite clip you'll see a single Sprite property under the Sprite Renderer, with one keyframe per frame of your animation. Here is our four-frame Run clip, drawn exactly as the window lays it out:

The Unity Animation window showing a sprite clip A recreation of Unity's Animation window. A toolbar at top holds playback controls, a clip dropdown reading Run, and a Samples field reading 12. Below, a property list on the left shows Sprite Renderer with a Sprite property. A timeline ruler marks frames 0, 5, 10, 15. On the Sprite track are four diamond keyframes with tiny sprite thumbnails, and a red playhead sits over the second keyframe. Animation Run Samples 12 Sprite Renderer Sprite run_01 0 5 10 15 Dopesheet Curves
Figure 1: The Animation window with a four-frame Run clip (faithfully recreated). Each diamond is a keyframe that swaps the Sprite Renderer's sprite. The red playhead shows which frame is currently displayed; the Samples field (12) sets how many frames play per second.
๐Ÿ’ก Why a diagram and not a screenshot? As you saw in Module 1, Unity's editor windows are drawn with a technology that doesn't screenshot cleanly, so throughout this course they're rebuilt as precise diagrams from the real setups โ€” every track, keyframe, and value matches what you'll see on screen.

Sample Rate & Looping

The Samples field in the toolbar (Figure 1 shows 12) is the clip's sample rate โ€” how many frames it plays per second. It's the single most important dial for the feel of a 2D animation:

  • Low sample rate (6โ€“8) gives a chunky, retro, "choppy on purpose" look โ€” think classic arcade sprites.
  • Higher sample rate (12โ€“24) gives smoother motion, but only if you actually have enough frames to fill it.

Sample rate does not add frames; it just changes how quickly Unity steps through the ones you have. Four frames at 12 samples means the run cycle repeats three times a second.

If the Samples field isn't visible, open the Animation window's overflow menu (the three dots, top-right) and enable Show Sample Rate.

๐Ÿ“– Definition

Looping: whether the clip restarts when it reaches the end. Select the .anim asset in the Project window and tick Loop Time in the Inspector. A run or idle cycle should loop; a one-shot like an attack or death animation should not.

โš ๏ธ Sample rate is not playback speed

You can also speed a clip up or down without re-baking it: in the Animator, select the state and change its Speed multiplier (1 = normal, 2 = double). Change the sample rate when you want the authored frame timing to differ; change the state Speed when you want the same clip to play faster or slower in one particular state.

Into the Animator State Machine

Here's the payoff, and the thread back to Module 1: a 2D sprite clip is just an Animation Clip, so it lives in an Animator Controller exactly like a 3D clip does. Everything you learned about states, transitions, parameters, and conditions applies unchanged.

Open the controller Unity created (double-click it, or Window โ–ธ Animation โ–ธ Animator) and you'll find your Run clip sitting as a state. Add an Idle clip the same way, then wire them up with the identical pattern from Lesson 1.2 โ€” an Idle โ‡„ Run pair driven by a Speed float, plus an Any State โ†’ Jump transition on a trigger:

flowchart LR Entry([Entry]) --> Idle Idle -- "Speed > 0.1" --> Run Run -- "Speed < 0.1" --> Idle Any([Any State]) -- "Jump trigger" --> Jump Jump -- "Exit Time" --> Idle Idle["Idle
(idle sprite clip)"] Run["Run
(4-frame run clip)"] Jump["Jump
(jump sprite clip)"]

Figure 2: The exact same state machine from Module 1 โ€” only now each state plays a sprite clip instead of a 3D clip. The Animator does not know or care whether a clip animates a skinned mesh or a Sprite Renderer.

This is the big idea of the lesson: you don't learn a separate "2D animation system." You reuse the one system Unity has, feeding it sprite clips instead of rigged-mesh clips.

โœ… Pro Tip

Set the Idle state as the default (right-click โ–ธ Set as Layer Default State) so it's orange and wired from Entry โ€” just as you did in Lesson 1.2. A character with no clear default sits on a blank frame and looks broken.

Driving It from Code

The controller reacts to parameters, and your script sets them โ€” using the new Input System (Package Manager โ–ธ Input System) rather than the legacy Input class. Below, a 2D character reads a move action, feeds its horizontal speed to the Animator, and flips the sprite to face its direction:

using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(Animator), typeof(SpriteRenderer))]
public class Sprite2DAnimDriver : MonoBehaviour
{
    Animator animator;
    SpriteRenderer sprite;

    // cached parameter hashes โ€” faster and typo-proof (see Lesson 1.2)
    static readonly int SpeedHash = Animator.StringToHash("Speed");
    static readonly int JumpHash  = Animator.StringToHash("Jump");

    Vector2 moveInput;

    void Awake()
    {
        animator = GetComponent<Animator>();
        sprite   = GetComponent<SpriteRenderer>();
    }

    // Wired to a "Move" action (Value / Vector2) via PlayerInput or an InputAction
    public void OnMove(InputAction.CallbackContext ctx)
    {
        moveInput = ctx.ReadValue<Vector2>();
    }

    public void OnJump(InputAction.CallbackContext ctx)
    {
        if (ctx.performed)
            animator.SetTrigger(JumpHash);   // fires Any State -> Jump once
    }

    void Update()
    {
        // drives the Idle <-> Run transitions
        animator.SetFloat(SpeedHash, Mathf.Abs(moveInput.x));

        // face the way we're moving by flipping the sprite, not rotating it
        if (moveInput.x > 0.01f) sprite.flipX = false;
        else if (moveInput.x < -0.01f) sprite.flipX = true;
    }
}

Two things worth noticing. First, the parameter names (Speed, Jump) are the same contract with the state machine you learned in Module 1 โ€” the Animator has no idea the clips are 2D. Second, we cache them as hashes with Animator.StringToHash, the exact production pattern promised back in Lesson 1.2.

โš ๏ธ Flip the sprite, don't rotate the object

To make a character face left, set spriteRenderer.flipX = true. Rotating the GameObject 180ยฐ on Y also mirrors it, but it fights your physics and can flip child objects (weapons, health bars) the wrong way. flipX mirrors only the sprite.

Hands-on Challenge

๐Ÿ‹๏ธ Exercise 1: Build a looping Run clip

Objective: Turn a strip of run frames into a playable, looping clip.

  1. Slice a run sprite sheet (Lesson 10.2) and confirm the frames are named in order (run_00 โ€ฆ run_07).
  2. Select your character, open the Animation window, and drag all run frames onto the timeline to create Run.anim.
  3. Set Samples to 12 and press play in the Animation window to preview.
  4. Select Run.anim in the Project window and tick Loop Time in the Inspector.
๐Ÿ’ก Hint: my run plays once and freezes

That's the Loop Time checkbox. It lives on the .anim asset in the Inspector, not in the Animation window. Tick it and the cycle repeats.

โœ… Success check

Pressing play in the Animation window shows a continuous, seamless run cycle at a natural pace. Lowering Samples to 6 makes it visibly chunkier; raising it to 24 makes it faster.

๐Ÿ‹๏ธ Exercise 2: Wire it into the Animator

Add an Idle clip and a Speed float, then recreate the Idle โ‡„ Run machine from Lesson 1.2 (Has Exit Time off on both). Attach Sprite2DAnimDriver, set up a Move action in an Input Actions asset, and confirm the character switches to Run when you press a movement key and flips to face its direction.

๐ŸŽฏ Quick Quiz

Question 1: What does dragging a sequence of sprite frames onto the Animation window create?

Question 2: Your four-frame run looks too fast. Which control slows the authored timing?

Question 3: How does a 2D sprite clip relate to the Animator state machine from Module 1?

Summary

๐ŸŽ‰ Key Takeaways

  • Frame-based 2D animation is a flipbook: a sequence of sprites swapped fast enough to read as motion.
  • Drag ordered frames onto the Animation window and Unity builds an Animation Clip with a keyframe per frame on the Sprite track.
  • Samples (sample rate) sets frames-per-second; Loop Time (on the asset) controls repetition.
  • A sprite clip is a normal Animation Clip, so it plugs into the same Animator state machine, parameters, and transitions you built in Module 1.
  • Drive it from code with the new Input System, cache parameter hashes, and use flipX to face direction.

๐Ÿš€ What's Next?

Drawing every frame by hand is expensive. What if you could pose a single drawing like a puppet โ€” bending an arm, turning a head โ€” instead of redrawing it? That's skeletal 2D animation. In Lesson 11.2: Skeletal 2D Animation you'll rig a sprite with bones in the Sprite Editor's Skinning module and animate it like a marionette.

๐ŸŽž๏ธ Your sprites are alive

You turned a row of still drawings into a running character and plugged it into the very same state machine that drives 3D characters. One animation system, two dimensions.