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

Godot中如何在角色停止移动后终止移动动画?

解决Godot角色停止移动后停止动画的问题

你的问题出在只处理了按键按下的动画触发逻辑,完全没处理按键松开后的状态判断,而且依赖_input事件的单次触发,无法实时跟踪持续按住/松开的状态。以下是两种可靠的解决方案:

方案一:用_physics_process实时检测输入状态(推荐)

把动画控制逻辑放到_physics_process(和移动逻辑同帧执行,同步性更好)里,每帧检测是否有移动按键被按住,动态切换动画:

func _physics_process(delta):
    # 检测所有移动按键的当前状态
    var is_moving = Input.is_action_pressed("move_right") || 
                    Input.is_action_pressed("move_left") || 
                    Input.is_action_pressed("move_up") || 
                    Input.is_action_pressed("move_down")

    if is_moving:
        # 仅当前不是行走动画时播放,避免重复调用play导致动画重置
        if $player_animator.current_animation != "walking_animation":
            $player_animator.play("walking_animation")
    else:
        # 停止行走动画,或切换到idle待机动画(推荐后者)
        if $player_animator.current_animation == "walking_animation":
            # $player_animator.play("idle_animation")
            $player_animator.stop()

方案二:在_input中处理按键松开事件

如果坚持用_input,需要在按键松开时检查所有移动按键是否都已松开,再停止动画:

var is_moving = false

func _input(event):
    # 处理按键按下
    if event.is_action_pressed("move_right") || event.is_action_pressed("move_left") || 
       event.is_action_pressed("move_up") || event.is_action_pressed("move_down"):
        is_moving = true
        if $player_animator.current_animation != "walking_animation":
            $player_animator.play("walking_animation")
    # 处理按键松开
    elif event.is_action_released("move_right") || event.is_action_released("move_left") || 
         event.is_action_released("move_up") || event.is_action_released("move_down"):
        # 重新校验所有移动按键状态,避免多按键时误判
        is_moving = Input.is_action_pressed("move_right") || 
                    Input.is_action_pressed("move_left") || 
                    Input.is_action_pressed("move_up") || 
                    Input.is_action_pressed("move_down")
        if not is_moving:
            $player_animator.stop()
            # 或切换到idle动画:$player_animator.play("idle_animation")

你之前方法无效的原因

  • stop方法未被正确触发:你只在按键按下时处理逻辑,松开时没有对应触发stop的代码。
  • is_running变量状态更新不及时:仅在按键按下时设为true,但松开时未重置或未每帧校验状态,导致变量无法正确反映当前移动状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 21:00:13