Godot 4: AnimationPlayer from a PNG Sequence
A folder of numbered PNGs is the most common output from an AI render, a Blender export, or a GIF splitter. Godot can animate it two ways:
- Pack the frames into one sheet and animate a
frameproperty on aSprite2D. - Keep the PNGs separate and animate the
textureproperty.
The first is almost always better for a game character. The second is useful for a short UI sequence or a handful of frames.
Path A: one sheet, one frame track
This is the recommended path.
1. Pack the frames
Use the sheet maker to pack the PNGs into a grid with uniform cells and 2 px padding. Keep the JSON; it tells you the columns, rows, and frame size.
If the frames have transparent edges, turn on trimming with uniform bounds so the character does not jitter.
2. Import the sheet
- Filter: Off for pixel art.
- Mipmaps: Off.
- Compress: Lossless.
3. Set up the Sprite2D
- Add a
Sprite2Dand assign the sheet as its texture. - Set Hframes and Vframes to the grid dimensions.
- The
frameproperty now selects a cell.
4. Build the animation
- Add an
AnimationPlayer. - Create an animation, for example
run. - Add a track for the Sprite2D’s
frameproperty. - Insert keys
0, 1, 2, …at the timing you want. - Set the track’s update mode to Discrete (not continuous).
- Enable looping on the animation.
extends Sprite2D
@onready var player: AnimationPlayer = $AnimationPlayer
func _ready() -> void:
player.play("run")
The discrete update mode is the one people miss. With continuous interpolation, Godot tries to blend frame 3.4, which is not a frame.
Path B: separate PNGs, texture track
Use this when you have a small number of frames that do not share a grid, or when the frames are UI elements of different sizes.
- Import the PNGs into the project.
- Add a
Sprite2D. - Add an
AnimationPlayer, create an animation. - Add a track for the Sprite2D’s
textureproperty. - Insert a key at each time, assigning the corresponding texture.
- Set the track to Discrete.
- Enable looping if needed.
extends Sprite2D
@onready var player: AnimationPlayer = $AnimationPlayer
func play_sequence() -> void:
player.play("ui_intro")
This works, but each PNG is a separate texture. For 30 character frames, that is 30 texture objects instead of one sheet, and more import bookkeeping. Pack them when you can.
Which node should own the animation?
| Situation | Use |
|---|---|
| Simple character loop | AnimatedSprite2D + SpriteFrames |
| Animation with hitboxes, sounds, VFX | AnimationPlayer |
| Short UI sequence | AnimationPlayer (texture or frame track) |
| Many animations with blending | AnimationTree |
The full comparison is in AnimatedSprite2D vs AnimationPlayer.
Timing and frame order
- At 12 FPS, each frame is
1/12 ≈ 0.0833 s. - At 24 FPS, each frame is
1/24 ≈ 0.0417 s. - The animation length is
frame_count / FPS.
If the frames are numbered 0001, 0002, … sort numerically, not lexicographically. frame_10.png sorts before frame_2.png in a naive string sort. The sheet maker uses natural sort.
Common problems
| Symptom | Cause | Fix |
|---|---|---|
| Frames blend into each other | Track update is continuous | Set it to Discrete |
| Animation plays too fast | Keys are one frame apart at the wrong FPS | Space keys by 1/FPS |
| Only the first frame shows | frame track has one key |
Add a key per frame |
| Frames are out of order | Lexicographic sort | Rename or pack with natural sort |
| Texture swap is offset | Different image sizes | Use a uniform sheet instead |
| Sprite is blurry | Filter on | Filter off for pixel art |
| Edge line appears | No padding | Add padding/extrude |
When to automate the setup
For one animation, the editor is fine. For dozens, generating the resource is faster and less error-prone. The SpriteFrames generator writes a .tres directly; for AnimationPlayer, a small editor script can create tracks, but that is a larger project.
A practical middle ground: use SpriteFrames for the character, and AnimationPlayer only for the timeline events. You get simple frame data and precise gameplay timing.
FAQ
Can I use a GIF directly? Not as a texture. Split it into frames or a sheet first; the sheet maker can decode a GIF in Chrome/Edge.
Do I need to import every PNG separately? Only for the texture-track path. The sheet path uses one texture.
How do I loop the animation?
Enable looping on the AnimationPlayer animation. For a one-shot, leave it off and connect animation_finished.
Can I change speed at runtime?
Yes: player.speed_scale = 1.5. It affects all tracks in that player.
What is the difference from AnimatedSprite2D? AnimatedSprite2D plays frame data; AnimationPlayer animates properties, including frames. Use the timeline when you need more than frames.
Next: Sprite sheet vs texture atlas — the vocabulary behind both paths.