You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot 4(GDScript)动画播放时机与切换控制问题

Godot 4 GDScript角色动画问题解决

核心问题分析

你当前代码中punch动画仅播放一瞬间就跳回idle的原因很明确:在_physics_process里,设置is_punching = true后,立刻播放punch动画并马上把is_punching设为false——这导致下一帧的动画判断直接进入idle逻辑,动画根本没机会完整播放。

以下是针对你两个核心问题的具体解决方案:


一、正确确定动画播放时机

动画播放时机需基于角色状态优先级判断,优先级从高到低建议:

  • 攻击/特殊动作 > 空中动作(跳、落) > 地面动作(蹲、跑、idle)
  • 每一帧先检查高优先级状态,只有当前状态不满足时,再进入低优先级状态判断。

二、确保动画播放完成后再切换

不要手动用定时器或直接重置状态结束动画,而是利用AnimatedSprite2D自带的animation_finished信号,监听动画播放完成事件后再切换状态。


修改后的关键代码实现

1. 调整状态判断逻辑

在_physics_process中保留is_punching判断,但不立刻重置状态:

# Handle punch
if Input.is_action_just_pressed("punch") and not is_punching:
    is_punching = true

# Determine animation state
if is_punching:
    # 仅当前动画不是punch时播放,避免重复触发
    if animated_sprite.animation != "punch":
        animated_sprite.play("punch")
elif is_on_floor() and not is_punching:
    # 原地面动画逻辑(idle、run、land、crouch)保持不变
    if not was_on_floor:
        animated_sprite.play("land")
        land_timer = 0.1
    elif land_timer > 0:
        land_timer -= delta
    elif direction == 0:
        animated_sprite.play("crouch" if is_crouching else "idle")
    else:
        animated_sprite.play("crouch" if is_crouching else "run")
    was_on_floor = true
    was_jumping_up = false
else:
    # 原空中动画逻辑保持不变
    if velocity.y < 0:
        if not was_jumping_up:
            animated_sprite.play("jump_up")
            was_jumping_up = true
    elif velocity.y > -100 and velocity.y < 100:
        animated_sprite.play("jump_max")
    else:
        animated_sprite.play("jump_down")
    was_on_floor = false

2. 添加动画完成信号监听

给AnimatedSprite2D节点连接animation_finished信号,在回调中重置is_punching:

# 脚本中新增信号回调函数
func _on_animated_sprite_2d_animation_finished():
    if animated_sprite.animation == "punch":
        is_punching = false

3. 可选:攻击时限制角色移动(优化体验)

若需要攻击时禁止角色移动,可在移动逻辑中添加判断:

# Handle movement
if direction and not is_punching:
    velocity.x = direction * SPEED
elif not is_punching:
    velocity.x = move_toward(velocity.x, 0, SPEED)
else:
    velocity.x = 0  # 攻击时锁定移动

额外注意事项

  • 确保编辑器中punch动画未勾选Loop属性(否则animation_finished信号永远不会触发)。
  • 可给攻击动作添加冷却,避免快速连续触发导致动画混乱:
    var attack_cooldown = 0.5
    var last_attack_time = 0.0
    
    # 处理punch输入时添加冷却判断
    if Input.is_action_just_pressed("punch") and not is_punching and (Time.get_ticks_msec() - last_attack_time) > attack_cooldown * 1000:
        is_punching = true
        last_attack_time = Time.get_ticks_msec()
    

内容的提问来源于stack exchange,提问作者dannkis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 16:04:58