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

Godot引擎问题:角色行走时无法跳跃

解决Godot角色行走时无法跳跃的问题

你的核心问题是把move_and_slide放在了_process函数里,所有物理相关逻辑(移动、跳跃、重力应用)必须统一在_physics_process中处理——_process的调用频率和物理引擎帧率不一致,会导致is_on_floor()的判断出现延迟或错误,行走时跳跃触发条件无法被正确检测。

以下是修正后的代码:

extends KinematicBody2D

var motion = Vector2()

func _physics_process(delta):
    # 处理水平移动
    if Input.is_action_pressed("ui_right"):
        motion.x = 120
        $Sprite.flip_h = false
        if is_on_floor():
            $AnimationPlayer.play("Move")
    elif Input.is_action_pressed("ui_left"):
        motion.x = -120
        $Sprite.flip_h = true
        if is_on_floor():
            $AnimationPlayer.play("Move")
    else:
        motion.x = 0
        if is_on_floor():
            $AnimationPlayer.play("Idle")

    # 应用重力
    if not is_on_floor():
        motion.y += 10
        if motion.y > 10:
            $AnimationPlayer.play("Fall")
        if motion.y < -10:
            $AnimationPlayer.play("Jump")

    # 处理跳跃(改用just_pressed避免持续触发)
    if is_on_floor() && (Input.is_action_just_pressed("ui_up") || Input.is_action_just_pressed("ui_select")):
        motion.y = -200

    # 物理移动必须在_physics_process中执行,同步角色状态
    motion = move_and_slide(motion, Vector2.UP)

关键修改说明:

  • 移除无用的_process函数,将move_and_slide移到_physics_process末尾,确保每次物理更新后同步角色位置,让is_on_floor()能准确检测地面状态。
  • 跳跃触发从is_action_pressed改为is_action_just_pressed,避免按住跳跃键时角色持续上升,符合正常跳跃逻辑。

内容的提问来源于stack exchange,提问作者M7md-Dev

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 17:45:36