Godot角色落地后无法移动问题求助(附代码)
问题解决
- 核心问题:重力逻辑仅在角色非落地时执行,导致落地后
velocity.y未被正确更新,地面碰撞交互干扰了水平移动。move_and_slide()需要持续的重力作用来协同处理地面贴合与水平移动。
修正步骤:
- 移除重力代码的
if !is_on_floor()判断,让重力持续作用于角色 - 可选:恢复
direction的normalized()调用,避免斜向移动速度过快
修正后的代码:
func _physics_process(delta): if not is_multiplayer_authority(): return # 持续应用重力,move_and_slide会自动处理地面碰撞时的垂直速度 velocity.y -= gravity * delta var input_dir = Input.get_vector("left", "right", "forward", "backward") var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() if direction: velocity.x = direction.x * SPEED velocity.z = direction.z * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) velocity.z = move_toward(velocity.z, 0, SPEED) print(is_on_floor()) move_and_slide()
解释:
- 角色落地后,
move_and_slide()会检测地面碰撞并自动将垂直方向速度(velocity.y)清零,同时保证水平移动正常响应。 - 原代码仅在非落地时应用重力,落地后
velocity.y可能残留负值,导致角色被"卡"在地面,水平移动速度被碰撞交互抵消。 - 恢复
normalized()可确保斜向移动速度与单方向一致,避免速度异常。
内容的提问来源于stack exchange,提问作者Giovanni Ottaviani
相关产品推荐
相关产品推荐

