Playing and controlling animations with AnimationPlayer node
The AnimationPlayer node is the primary way to play and control animations in Godot. It manages animation playback, blending between animations, and provides extensive control over timing and playback.
The AnimationPlayer provides simple methods for controlling animation playback:
# Play an animation$AnimationPlayer.play("walk")# Play with custom speed (2x speed)$AnimationPlayer.play("walk", -1, 2.0)# Play backwards$AnimationPlayer.play_backwards("run")# Pause the current animation$AnimationPlayer.pause()# Stop and reset$AnimationPlayer.stop()
# Get currently playing animationvar current = $AnimationPlayer.current_animationprint("Playing: ", current)# Set current animation (doesn't auto-play)$AnimationPlayer.current_animation = "idle"# Get assigned animationvar assigned = $AnimationPlayer.assigned_animation
current_animation changes the animation but doesn’t play it unless already playing. Use play() to start playback.
# Check if playingif $AnimationPlayer.is_playing(): print("Animation is active")# Get playback positionvar position = $AnimationPlayer.current_animation_positionprint("Current time: ", position)# Get animation lengthvar length = $AnimationPlayer.current_animation_lengthprint("Total duration: ", length)# Get actual playing speed (speed_scale × custom_speed)var speed = $AnimationPlayer.get_playing_speed()
# Set default blend time for all transitions$AnimationPlayer.playback_default_blend_time = 0.2# Now all animations blend smoothly over 0.2 seconds$AnimationPlayer.play("walk")await get_tree().create_timer(1.0).timeout$AnimationPlayer.play("run") # Blends from walk to run
Set specific blend times between particular animations:
# Blend from "idle" to "walk" over 0.3 seconds$AnimationPlayer.set_blend_time("idle", "walk", 0.3)# Blend from "walk" to "run" over 0.1 seconds$AnimationPlayer.set_blend_time("walk", "run", 0.1)# Blend from "run" to "idle" over 0.5 seconds$AnimationPlayer.set_blend_time("run", "idle", 0.5)# Play with automatic blending$AnimationPlayer.play("walk") # Uses custom blend time
# Play from 0.5 to 2.0 seconds$AnimationPlayer.play_section("walk", 0.5, 2.0)# Play section using markers$AnimationPlayer.play_section_with_markers("cutscene", "start", "end")# Update section boundaries while playing$AnimationPlayer.set_section(1.0, 3.0)# Reset to play full animation$AnimationPlayer.reset_section()
# Play current animation, then queue others$AnimationPlayer.play("attack")$AnimationPlayer.queue("idle")# Check the queuevar queued = $AnimationPlayer.get_queue()print("Queued animations: ", queued)# Clear the queue$AnimationPlayer.clear_queue()
Automatically play another animation when one finishes:
# When "attack" finishes, automatically play "idle"$AnimationPlayer.animation_set_next("attack", "idle")# Get the next animationvar next = $AnimationPlayer.animation_get_next("attack")print("After attack, plays: ", next)