Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 36 ยท UI, Audio and Animation

AnimationPlayer and Tweens

Animate anything in Godot 4: AnimationPlayer tracks and keyframes, method call tracks, code tweens with create_tween, easing and transitions, and AnimationTree blending, with Python stand-ins.

Godot has two main ways to animate. AnimationPlayer records keyframes in the editor and can animate any property of any node: positions, colors, sprite frames, even function calls. Tweens are created in code and animate a property from its current value to a target over time, which is perfect for UI, pickups and juice. Learn both, and add AnimationTree when you need state machines and blending.

The Godot editor cannot run on this site, so engine code is read-only; runnable blocks are Python stand-ins that model keyframe interpolation and easing. Before this lesson see control nodes and containers.

AnimationPlayer: tracks and keyframes

An AnimationPlayer owns an AnimationLibrary of named Animations. Each animation has a length and several tracks:

  • Property tracks change a property (position, modulate, frame, visible).
  • Method Call tracks call a function at a given time: spawn a bullet at the moment of a sword swing.
  • Audio tracks play a sound in sync.
  • Bezier curve tracks give smooth, hand-shaped curves.
  • Animation Playback tracks start other animations.

In the editor, open the Animation panel, click Animation > New, press the key icon next to a property in the Inspector to add a keyframe, then move the timeline and set the value again. Each track has an interpolation mode (nearest, linear, cubic) and a loop mode. Play from code:

GDSCRIPT
@onready var anim: AnimationPlayer = $AnimationPlayer

func attack() -> void:
	anim.play("attack")
	await anim.animation_finished
	anim.play("idle")

func _ready() -> void:
	anim.speed_scale = 1.5       # play 50 percent faster

play_backwards("open") reuses the same animation, and RESET is a special animation that stores the default values the editor restores when you stop previewing.

Python Stand-in (Python, not Godot): linear interpolation between keyframes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

An animation player evaluates exactly this: find the two keyframes around the current time and interpolate between them.

Tweens: animation in code

A Tween is a short-lived object that changes properties for you. Create it with create_tween() (bound to the node, so it stops when the node is freed):

GDSCRIPT
func pop_in() -> void:
	scale = Vector2.ZERO
	var tween := create_tween()
	tween.tween_property(self, "scale", Vector2.ONE, 0.35) \
		.set_trans(Tween.TRANS_BACK).set_ease(Tween.EASE_OUT)

func hit_flash() -> void:
	var tween := create_tween()
	tween.tween_property($Sprite2D, "modulate", Color.RED, 0.05)
	tween.tween_property($Sprite2D, "modulate", Color.WHITE, 0.15)

By default tweeners run one after another. Call tween.set_parallel(true) to run them together, tween.chain() to return to sequence, tween_interval(0.5) to wait, tween_callback(queue_free) to run code at the end, and set_loops(3) to repeat (0 is infinite). Kill a running tween with tween.kill() before starting a competing one, or two tweens will fight over the same property.

Easing and transitions

A tween is described by two choices: a transition (the curve shape: Linear, Sine, Quad, Cubic, Back, Elastic, Bounce) and an ease (In, Out, InOut, OutIn). Ease out starts fast and settles gently, which feels natural for things arriving; ease in starts slowly.

Python Stand-in (Python, not Godot): common easing curves

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

back_out overshoots above 1.0 before settling, which is why TRANS_BACK gives that bouncy "pop". The numbers are the standard easing formulas; Godot's built-in curves follow the same shapes.

Python Stand-in (Python, not Godot): a sequence then a parallel group of tweens

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

AnimationTree: state machines and blending

When a character has many animations (idle, walk, run, jump, fall, attack) switching with play() gets messy. AnimationTree sits next to an AnimationPlayer and adds:

  • an AnimationNodeStateMachine with states and transitions you can travel between with playback.travel("run");
  • BlendSpace1D/2D nodes that mix animations by a parameter, such as blending idle to walk to run by speed;
  • Blend2, Add2, OneShot nodes for layering an attack on top of a running animation.
GDSCRIPT
@onready var tree: AnimationTree = $AnimationTree
@onready var playback: AnimationNodeStateMachinePlayback = tree["parameters/playback"]

func _physics_process(_delta: float) -> void:
	tree["parameters/Move/blend_position"] = velocity.length() / SPEED
	if not is_on_floor():
		playback.travel("Fall")

Blending is a weighted average of poses:

Python Stand-in (Python, not Godot): BlendSpace1D mixes two animations by speed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Which tool when?

  • AnimationPlayer for authored, repeatable animations with sound and method calls.
  • Tween for one-off, code-driven effects with runtime target values.
  • AnimationTree for characters with many states.
  • _process with lerp for continuous following (see camera smoothing).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which AnimationPlayer track type calls a function at a chosen time?

    Method Call tracks run a function on a node when the timeline reaches the key, e.g. to spawn a projectile mid-swing.
  2. 2. Tween tweeners added one after another run:

    Sequence is the default; set_parallel(true) switches to simultaneous.
  3. 3. Which transition gives an overshoot 'pop' effect?

    TRANS_BACK goes past the target and returns, especially with EASE_OUT.
  4. 4. Why kill() an existing tween before starting a new one on the same property?

    Each tween writes the property every frame; the last writer wins and the result flickers.

Summary

  • AnimationPlayer keyframes any property and can call methods and play audio.
  • create_tween() animates properties from code with sequence, parallel, loops and callbacks.
  • Transition plus ease define the feel; Back and Elastic overshoot.
  • AnimationTree adds state machines and blending for characters.

Next: Audio, Buses and Sound