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

如何修复Godot中3D第三人称平台游戏的跳跃代码异常问题

解决Godot 3D第三人称跳跃异常的问题

看起来你遇到的跳跃问题根源很明确——每次物理帧都把y方向的速度强制清零了,这直接打断了重力的累积和跳跃力的持续作用,导致玩家只能微小起跳后缓慢下落。

问题代码分析

看你_physics_process函数的前两行:

vel.x = 0
vel.y = 0

你把vel.y设为0的操作会在每帧重置垂直方向的速度,哪怕上一帧已经通过重力或者跳跃积累了速度。这就意味着:

  • 跳跃时,你刚给vel.y加上jumpForce,下一帧就会被清零,根本没法获得持续的上升动力
  • 下落时,重力的加速度也无法持续累积,导致下落速度异常缓慢

修复方案

只重置水平方向(x、z)的速度,保留垂直方向(y)的速度,让重力和跳跃力能正常作用:

修改后的_physics_process函数:

func _physics_process(delta):
    # 只重置水平方向速度,保留垂直方向速度
    vel.x = 0
    vel.z = 0
    var input = Vector3()
    if Input.is_action_pressed("move_forward"):
        input.z += 1
    if Input.is_action_pressed("move_backward"):
        input.z -= 1
    if Input.is_action_pressed("move_left"):
        input.x += 1
    if Input.is_action_pressed("move_right"):
        input.x -= 1
    input = input.normalized()
    var dir = (transform.basis.z * input.z + transform.basis.x * input.x)
    vel.x = dir.x * moveSpeed
    vel.z = dir.z * moveSpeed
    vel.y -= gravity * delta
    if Input.is_action_pressed("jump") and is_on_floor():
        vel.y += jumpForce
    vel = move_and_slide(vel, Vector3.UP)

额外优化建议

  • 可以调整jumpForce和gravity的数值,比如把jumpForce调到15-20,gravity保持15或者稍高,让跳跃手感更自然
  • 建议使用Input.is_action_just_pressed("jump")代替is_action_pressed,这样玩家按住跳跃键不会连续起跳,符合常规平台跳跃游戏的逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 02:59:04