godot-animation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGodot Animation (4.x)
Godot 动画(4.x)
Choose and drive the right animation tool: (clips),
(blending/state machines), or (short procedural moves). Targets Godot 4.3+.
AnimationPlayerAnimationTreeTween选择并使用合适的动画工具:(片段动画)、(混合/状态机)或(短流程移动动画)。适配**Godot 4.3+**版本。
AnimationPlayerAnimationTreeTweenWhen to use
使用场景
- Use when playing keyframed animations, blending walk/run/idle states, animating a sprite sheet, or tweening UI/objects in code.
When not to use: the movement logic that decides which state to play →
; UI layout (vs. UI tweening) → ; shader-driven
effects → .
godot-2d-movementgodot-ui-controlgodot-shaders- 适用于播放关键帧动画、混合行走/奔跑/idle状态、制作精灵表动画,或通过代码为UI/对象添加补间动画的场景。
不适用于:决定播放哪个状态的移动逻辑 → 参考;UI布局(而非UI补间动画)→ 参考;着色器驱动的特效 → 参考。
godot-2d-movementgodot-ui-controlgodot-shadersCore workflow
核心工作流程
- Pick the tool:
- — author keyframed clips (transforms, properties, method calls, audio, even other animations). The source of truth for clip data.
AnimationPlayer - — blend and transition between those clips at runtime with a state machine and/or blend spaces. Needs an
AnimationTreeto pull clips from.AnimationPlayer - — fire-and-forget procedural interpolation in code (
Tween); ideal for UI pops, fades, and one-off moves.create_tween()
- For 2D sprite sheets: use +
AnimatedSprite2D, or keyframe theSpriteFramesproperty in anframe.AnimationPlayer - For characters: build clips in , then add an
AnimationPlayer(AnimationTree), set itsactive = true, and design ananim_playerorAnimationNodeStateMachineas the tree root.AnimationNodeBlendSpace2D - Drive transitions from code via the playback object () or by setting blend parameters.
travel - React to clip events with the signal and call/method tracks.
animation_finished
- 选择工具:
- — 制作关键帧片段动画(变换、属性、方法调用、音频,甚至其他动画),是片段动画数据的权威来源。
AnimationPlayer - — 在运行时通过状态机和/或混合空间混合并切换这些片段动画,需要从
AnimationTree获取片段动画数据。AnimationPlayer - — 通过代码中的
Tween实现一次性流程插值动画;非常适合UI弹出、淡入淡出和一次性移动效果。create_tween()
- 2D精灵表动画:使用+
AnimatedSprite2D,或在SpriteFrames中为AnimationPlayer属性设置关键帧。frame - 角色动画:在中制作片段动画,然后添加
AnimationPlayer(设置AnimationTree),指定其active = true,并将anim_player或AnimationNodeStateMachine设为树的根节点。AnimationNodeBlendSpace2D - 通过代码驱动切换:通过播放对象(方法)或设置混合参数实现。
travel - 响应片段动画事件:使用信号和调用/方法轨道。
animation_finished
Patterns
实践模式
1. AnimationPlayer: play a clip and await it
1. AnimationPlayer:播放片段动画并等待结束
gdscript
@onready var anim: AnimationPlayer = $AnimationPlayer
func attack() -> void:
anim.play("attack")
await anim.animation_finished # resume after the clip ends
anim.play("idle")gdscript
@onready var anim: AnimationPlayer = $AnimationPlayer
func attack() -> void:
anim.play("attack")
await anim.animation_finished # 片段动画结束后继续执行
anim.play("idle")2. AnimationTree state machine: travel between states
2. AnimationTree状态机:在状态间切换
gdscript
@onready var tree: AnimationTree = $AnimationTree
func _ready() -> void:
tree.active = true # the tree drives animation; AnimationPlayer is the source
func set_state(state: StringName) -> void:
# The playback object controls an AnimationNodeStateMachine root.
var sm: AnimationNodeStateMachinePlayback = tree.get("parameters/playback")
sm.travel(state) # transitions using the graph's connections
func _physics_process(_d: float) -> void:
set_state(&"run" if velocity.length() > 5.0 else &"idle")gdscript
@onready var tree: AnimationTree = $AnimationTree
func _ready() -> void:
tree.active = true # 由树驱动动画;AnimationPlayer是数据源
func set_state(state: StringName) -> void:
# 播放对象控制AnimationNodeStateMachine根节点
var sm: AnimationNodeStateMachinePlayback = tree.get("parameters/playback")
sm.travel(state) # 通过图中的连接进行状态切换
func _physics_process(_d: float) -> void:
set_state(&"run" if velocity.length() > 5.0 else &"idle")3. Blend space: blend run direction by a 2D parameter
3. 混合空间:通过2D参数混合奔跑方向
gdscript
undefinedgdscript
undefinedRoot is an AnimationNodeBlendSpace2D named "Move" with idle/run points placed in it.
根节点是名为"Move"的AnimationNodeBlendSpace2D,其中包含idle/run动画点
func update_locomotion(input_dir: Vector2) -> void:
# Parameter path = "parameters/<node name>/blend_position".
tree.set("parameters/Move/blend_position", input_dir)
undefinedfunc update_locomotion(input_dir: Vector2) -> void:
# 参数路径 = "parameters/<节点名称>/blend_position"
tree.set("parameters/Move/blend_position", input_dir)
undefined4. Tween: fade and scale a UI element (code-driven)
4. Tween:为UI元素添加淡入和缩放效果(代码驱动)
gdscript
func pop_in(node: Control) -> void:
node.scale = Vector2.ZERO
node.modulate.a = 0.0
var tw := create_tween() # bound to this node's tree
tw.set_parallel(true) # run the two tweens together
tw.tween_property(node, "scale", Vector2.ONE, 0.2) \
.set_trans(Tween.TRANS_BACK).set_ease(Tween.EASE_OUT)
tw.tween_property(node, "modulate:a", 1.0, 0.2) # sub-property via ":"gdscript
func pop_in(node: Control) -> void:
node.scale = Vector2.ZERO
node.modulate.a = 0.0
var tw := create_tween() # 绑定到当前节点的树
tw.set_parallel(true) # 同时运行两个补间动画
tw.tween_property(node, "scale", Vector2.ONE, 0.2) \
.set_trans(Tween.TRANS_BACK).set_ease(Tween.EASE_OUT)
tw.tween_property(node, "modulate:a", 1.0, 0.2) # 通过":"指定子属性Pitfalls
常见陷阱
- left false → nothing animates and
AnimationTree.activeappears to do nothing. Settravel()and assign theactive = truepath.anim_player - Wrong parameter path. Blend/condition paths are and must match the node names in the tree exactly (e.g.
"parameters/<NodeName>/...","parameters/playback"). A typo silently no-ops."parameters/Move/blend_position" - AnimationPlayer and AnimationTree fighting. When an is active, don't also call
AnimationTreefor the same tracks — let the tree own playback.AnimationPlayer.play() - 3.x Tween node is gone. There is no node to add in 4.x; create tweens in code with
Tween(which returns acreate_tween()). They auto-start and free themselves.Tween - Reusing a finished tween. A tween is one-shot; call again for a new animation. Use
create_tween()for repetition.set_loops() - /
yieldis gone. Useyield(anim, "...").await anim.animation_finished - Sub-property tweens use a colon: ,
"modulate:a". Tweening the whole property instead overwrites siblings."position:x"
- 设为false → 不会播放任何动画,
AnimationTree.active看似无效。需设置travel()并指定active = true路径。anim_player - 参数路径错误。混合/条件路径格式为,必须与树中的节点名称完全匹配(例如
"parameters/<NodeName>/..."、"parameters/playback")。拼写错误会导致无提示失效。"parameters/Move/blend_position" - AnimationPlayer与AnimationTree冲突。当处于激活状态时,不要对同一轨道调用
AnimationTree— 让树控制播放。AnimationPlayer.play() - 3.x版本的Tween节点已移除。4.x版本中没有可添加的节点;需通过代码调用
Tween创建补间动画(返回一个create_tween()对象)。它们会自动启动并自行释放。Tween - 重复使用已完成的补间动画。补间动画是一次性的;如需新动画,请再次调用。使用
create_tween()实现重复播放。set_loops() - /
yield已移除。请使用yield(anim, "...")。await anim.animation_finished - 子属性补间使用冒号:、
"modulate:a"。如果补间整个属性,会覆盖其他子属性。"position:x"
References
参考资料
- For state machine transitions/conditions, root motion, one-shot/blend nodes, method &
call tracks, /
SpriteFrames, and Tween easing/chaining/callbacks, readAnimatedSprite2D.references/animation-tree-and-tween.md
- 关于状态机切换/条件、根运动、一次性/混合节点、方法与调用轨道、/
SpriteFrames以及Tween的缓动/链式调用/回调,请阅读AnimatedSprite2D。references/animation-tree-and-tween.md
Related skills
相关技能
- — supplies the velocity/state that selects animations.
godot-2d-movement - — UI that Tweens animate.
godot-ui-control - — 3D character scenes driven by AnimationTree.
godot-3d-essentials - — state machines that mirror animation states.
game-ai
- — 提供选择动画所需的速度/状态。
godot-2d-movement - — 可通过Tween制作动画的UI。
godot-ui-control - — 由AnimationTree驱动的3D角色场景。
godot-3d-essentials - — 与动画状态对应的状态机。
game-ai