A still sprite looks fine in a screenshot, and dead in a game. The moment your character should walk, the picture has to change, frame by frame, about ten times a second. In Godot 4 the node that does that job is AnimatedSprite2D, and the list of pictures it cycles through lives in a SpriteFrames resource. This post builds a small character with three animations, idle, walk and jump, in six steps, with code tested in Godot 4.3. If you would rather watch the lesson, the video version is in the Godot 4 Full Course in Hindi, free on YouTube.
The two pieces you need
Animation in Godot 4 has two halves. AnimatedSprite2D is the node in your scene: it draws one frame at a time and knows which animation is playing. SpriteFrames is a resource that holds the pictures, grouped into named animations, each with its own speed. One node holds one SpriteFrames resource, and that resource can hold as many animations as you like, so one character node can carry idle, walk and jump.
Step 1: add an AnimatedSprite2D to your player
Start with the player scene from how to move a character in Godot 4 with GDScript, a CharacterBody2D with a CollisionShape2D. Select the player, add a child node, and pick AnimatedSprite2D. If the player already has a plain Sprite2D, delete it, because two nodes drawing the same character will overlap.
The AnimatedSprite2D must sit under the body that moves, so it follows the player instead of staying behind on the floor.
Step 2: make a SpriteFrames resource and add your frames
Select the AnimatedSprite2D and look at the Inspector. The first property under Animation is Sprite Frames, and it is empty. Click it, pick New SpriteFrames, and a SpriteFrames panel opens at the bottom of the editor.
On the left is the animations list, starting with one animation called default. In the middle is the frame strip, empty for now. Drag your pictures from the FileSystem dock into the frame strip. For this post I drew eight small frames with a script: two for idle, four for walk, two for jump. If you want real game art instead of drawn shapes, grab our free Catch the Fruit art pack and drop its fruit pictures in the same way. The steps are identical whatever pictures you use. Godot's 2D sprite animation tutorial covers the same panel with its own pictures.
Step 3: name the animations and set the speed
One animation called default tells you nothing. Rename it to idle, then click Add Animation and make walk and jump the same way, dragging the matching frames into each. The name is what your code asks for later, so spell it once and use that spelling everywhere. A typo here is the most common reason an animation refuses to play, and the error names the missing animation.
Each animation also has a speed, in frames per second. Idle runs at 5 FPS here, slow enough to read as breathing. Walk runs at 10 FPS, fast enough to read as steps. Jump runs at 8 FPS. Speed belongs to the animation, not the node, so idle and walk can run at different rates. Press Play at the top right of the panel to preview, and adjust the number until the motion feels right.
Step 4: play the right animation from code
The editor shows your pictures. Your code decides when each animation runs. Attach this script to the player:
extends CharacterBody2D
@export var speed = 300.0
@onready var animated_sprite = $AnimatedSprite2D
func _physics_process(_delta):
var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
velocity = direction * speed
update_animation(direction)
move_and_slide()
func update_animation(direction):
if direction.x != 0:
animated_sprite.flip_h = direction.x < 0
if direction != Vector2.ZERO:
animated_sprite.play("walk")
else:
animated_sprite.play("idle")Those two calls are the whole trick: you never touch individual frames in code, you only name the animation you want.
One detail worth knowing: calling play("walk") on every physics frame does not restart the cycle. Godot keeps playing from the frame it already reached. I checked this in a headless test on Godot 4.3, where calling play() again left the frame and the progress exactly where they were. The first 2D game tutorial uses the same pattern.
Step 5: flip the sprite instead of drawing it twice
Our walk cycle faces right. For walking left, you do not need a second set of pictures. AnimatedSprite2D has a flip_h property, and the script sets it from the direction: true when the player moves left, false when moving right. One line, and the same four frames serve both directions.
There is a matching flip_v for vertical flips, but a walk cycle flipped upside down looks wrong, so save it for things like a projectile flying downward.
Step 6: play a one-shot animation and catch the end
Idle and walk loop forever, which is what you want for them. A jump should play once and hand control back to idle. That is what the animation_finished signal is for: it fires when a non-looping animation reaches its last frame, and stays quiet while a looping animation runs.
Set the jump animation to not loop in the SpriteFrames panel, then play it like this:
func play_jump():
animated_sprite.play("jump")
await animated_sprite.animation_finished
animated_sprite.play("idle")The await line pauses the function until the signal fires, then the next line runs. In the headless test the signal fired with the animation stopped on its last frame, and play("idle") started the breathing loop again. Signals work the same way for button presses and coin pickups, and how Godot signals work explains the pattern in full.
AnimatedSprite2D, Sprite2D, or AnimationPlayer
Godot has three ways to show a moving picture:
| Node | Best for | What it cannot do |
|---|---|---|
| AnimatedSprite2D | Frame-by-frame character animation | Move anything except the picture itself |
| Sprite2D | One still picture, or a picture you move by hand | Play a sequence of frames on its own |
| AnimationPlayer | Moving, fading and scaling several things at once | Keep frame lists as simple as SpriteFrames |
For a character that walks, idles and jumps, AnimatedSprite2D is the right tool, because the whole job is choosing which list of pictures to show. Reach for AnimationPlayer when one animation has to move the node and change its look at the same time.
Five mistakes that stop animations working
Most animation bugs look the same on screen: the character stands still. Work through this list in order.
First, the animation name in code does not match the name in the panel. The names are case sensitive, and "Walk" is not "walk". Read the error message before changing anything else: it names the missing animation.
Second, the SpriteFrames resource was never assigned. Its Sprite Frames property is empty, so there is nothing to play. Select the AnimatedSprite2D and check the Inspector.
Third, the speed is wrong for the feeling you want. A walk at 2 FPS looks like a slideshow, and an idle at 30 FPS looks like shivering. Start at 8 to 10 FPS for a walk and 4 to 5 for an idle.
Fourth, the frames are in the wrong order. Drag the frames in the strip until the order reads left foot, together, right foot, together, or the character looks like it is tripping.
Fifth, animation_finished never fires. It only fires for animations that do not loop. A looping jump never sends the signal, so check the loop checkbox, or the await after it waits forever.
Questions beginners actually ask
How many frames does a walk cycle need?
Four is enough: left foot forward, feet together, right foot forward, feet together. Two can work for a simple top-down character. More than eight is usually wasted effort, because the frames flash past too fast to see.
Can one AnimatedSprite2D hold several characters?
It can hold as many animations as you like, but one node draws one character. Give each character its own node and its own SpriteFrames resource.
Why does my character slide without moving its legs?
The movement code runs but the animation never starts, which usually means play() is never called, or something calls stop() after it. Check that your update function runs every physics frame.
Should I use AnimatedSprite2D or AnimationPlayer for a player character?
Start with AnimatedSprite2D. It covers idle, walk, jump and attack with the least setup. Move to AnimationPlayer when one animation has to move the node and change its look at the same time.
Your turn
You now have a character that breathes when it stands still, walks when it moves, and jumps once when you tell it to. Add a fourth animation to the same SpriteFrames resource, an attack or a hurt pose, and wire it to a key press the way Step 6 wired the jump. When you are ready for the next lesson, the Godot 4 Full Course in Hindi, free on YouTube picks up from here.
Leave a Comment