AnimationTree & State Machines
🎬 AnimationTree, State Machines & BlendTrees in Godot 4
While AnimationPlayer handles raw keyframe playback, the AnimationTree node evaluates real-time skeletal blending, directional blendspaces (walk/run directions), and complex gameplay state transitions.
🏛️ Root Node Modes of AnimationTree
+-------------------------------------------------------------+
| AnimationTree (Tied to target AnimationPlayer) |
+-------------------------------------------------------------+
│
├── 1. AnimationNodeStateMachine (Discrete states: Idle, Run, Jump, Attack)
│
├── 2. AnimationNodeBlendTree (Node graph: Additive blends, Filtered bones)
│
└── 3. AnimationNodeBlendSpace2D (Directional 2D joystick blending)
💻 1. Controlling AnimationTree from GDScript
# CharacterAnimator.gd
extends CharacterBody3D
@onready var anim_tree: AnimationTree = $AnimationTree
@onready var state_machine: AnimationNodeStateMachinePlayback = anim_tree.get("parameters/playback")
func _physics_process(delta: float) -> void:
var speed: float = velocity.length()
if is_on_floor():
if speed > 0.1:
state_machine.travel("Walk")
# Update 2D BlendSpace coordinates for 8-way directional movement
var local_vel: Vector3 = global_transform.basis.inverse() * velocity
anim_tree.set("parameters/Walk/blend_position", Vector2(local_vel.x, local_vel.z).normalized())
else:
state_machine.travel("Idle")
else:
state_machine.travel("Jump_Fall")
func trigger_melee_attack() -> void:
# One-shot animation overlay
state_machine.travel("Attack_Slash")
🔗 Related Notes
- 3D Animation & Rigging — Skeletal bone hierarchies and FK/IK.
- Getting Started With 3D Animation — Animation principles and the bouncing ball test.
- Animation Blueprints & State Machines — Comparison with Unreal Engine 5 AnimGraphs.