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

Godot引擎KinematicBody无法沿Z轴移动问题求助

问题排查与修复方案
  • 最可能的直接原因:输入映射配置缺失/错误
    请优先检查Godot项目设置中的输入映射表,确认forward、back两个动作是否已经正确绑定了对应按键,且动作名称和代码中拼写完全一致(注意大小写敏感)。你提到X轴移动正常,说明left和right的输入映射是没问题的,遗漏前后移动的输入配置是这类问题最高发的原因。
  • 隐性兼容问题:Godot版本适配错误
    如果你使用的是Godot 4.x版本,你的代码写法存在兼容问题:Godot 4中move_and_slide()不再返回处理后的velocity向量,而是返回表示是否发生碰撞的布尔值,也不需要传入velocity和向上向量作为参数,直接操作节点自带的velocity属性后调用无参的move_and_slide()即可。如果在Godot 4中沿用Godot 3的写法,会导致velocity被错误赋值,不过这个问题一般会导致所有方向都无法移动,如果只有Z轴异常可以延后排查该点。
  • 现有代码的优化修复
    你的代码还存在斜向移动速度异常的隐性问题,同时分量赋值的写法容易出现疏漏,可参考下方修改后的代码调整:
# Godot 3.x 版本可用
func _physics_process(delta):
    velocity.y += gravity * delta
    _get_input()
    velocity = move_and_slide(velocity, Vector3.UP)

func _get_input():
    var dir = Vector3.ZERO
    if Input.is_action_pressed("forward"):
        dir -= transform.basis.z
    if Input.is_action_pressed("back"):
        dir += transform.basis.z
    if Input.is_action_pressed("left"):
        dir -= transform.basis.x
    if Input.is_action_pressed("right"):
        dir += transform.basis.x
    
    # 归一化处理避免斜向移动速度过快
    if dir.length() > 0:
        dir = dir.normalized() * speed
    
    # 保留重力的y轴速度,直接覆盖水平分量
    velocity.x = dir.x
    velocity.z = dir.z

如果是Godot 4.x版本,可使用下方适配后的代码:

# Godot 4.x 版本可用
func _physics_process(delta):
    velocity.y += gravity * delta
    _get_input()
    move_and_slide()

func _get_input():
    var dir = Vector3.ZERO
    if Input.is_action_pressed("forward"):
        dir -= transform.basis.z
    if Input.is_action_pressed("back"):
        dir += transform.basis.z
    if Input.is_action_pressed("left"):
        dir -= transform.basis.x
    if Input.is_action_pressed("right"):
        dir += transform.basis.x
    
    if dir.length() > 0:
        dir = dir.normalized() * speed
    
    velocity.x = dir.x
    velocity.z = dir.z

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 13:48:00