Administrator
发布于 2026-11-28 / 16 阅读
0

Godot 4 用 CharacterBody2D 调出跟手的跳跃手感

今天要解决的问题是让角色在 Godot 4 中实现自然流畅的跳跃。学完后你可以创建一个具有真实物理特性的 2D 角色,并根据实际需求调整跳跃的灵敏度。

准备

本教程需要 Godot 4.3 和 GDScript 2.0 环境。准备一个包含 CharacterBody2D 组件的角色节点,并确保场景树中已连接到主控脚本文件。若尚未安装 Godot 4.3,请从 https://godotengine.org/download 获取并安装。打开项目后,在编辑器中创建一个新的 Scene,选择 CharacterBody2D 节点作为根节点,并为其添加一个脚本文件(右键点击节点 > 添加脚本)。然后在主菜单中使用 File > Export Project 设置导出路径为 /home/user/projects/godot_jump_test 并保存配置。

步骤1:move_and_slide 与 is_on_floor 的正确用法

以下是一个简单的脚本示例,用于设置 CharacterBody2D 的基本移动逻辑。

extends CharacterBody2D

@export var speed: float = 200.0
@export var gravity: float = -900.0
var velocity = Vector2.ZERO

func _physics_process(delta: float) -> void:
    velocity.y += gravity * delta
    if Input.is_action_pressed("ui_right"):
        velocity.x = speed
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -speed
    else:
        velocity.x = lerp(velocity.x, 0, 0.1)

    move_and_slide(velocity, Vector2.UP)

    # 检查是否站在地面上
    if is_on_floor():
        print("站在地面上")
    else:
        print("空中")

预期输出:控制台会打印 "站在地面上" 或 "空中",表示角色的地面检测生效。

步骤2:重力与跳跃速度的量纲换算

接下来为角色添加跳跃功能,并设置合适的初始速度值。

extends CharacterBody2D

@export var speed: float = 200.0
@export var gravity: float = -900.0
@export var jump_force: float = -300.0
var velocity = Vector2.ZERO

signal jumped

func _ready() -> void:
    # 设置输入动作
    Input.map_action("jump", [InputEventKey.new(keycode=KEY_SPACE)])

func _physics_process(delta: float) -> void:
    velocity.y += gravity * delta
    if Input.is_action_pressed("ui_right"):
        velocity.x = speed
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -speed
    else:
        velocity.x = lerp(velocity.x, 0, 0.1)

    # 跳跃逻辑
    if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y = jump_force
        jumped.emit()

    move_and_slide(velocity, Vector2.UP)

    if is_on_floor():
        print("站在地面上")
    else:
        print("空中")

预期输出:角色在按下空格键时会跳起,同时控制台打印 "站在地面上" 表示成功触地。

步骤3:土狼时间与跳跃缓冲的实现

为了使跳跃更加自然,可以添加一个 input_buffer 属性,用于处理跳跃的缓冲机制。

extends CharacterBody2D

@export var speed: float = 200.0
@export var gravity: float = -900.0
@export var jump_force: float = -300.0
@export var input_buffer_time: float = 0.2
var velocity = Vector2.ZERO
var input_buffer = false

signal jumped

func _ready() -> void:
    # 设置输入动作
    Input.map_action("jump", [InputEventKey.new(keycode=KEY_SPACE)])

func _process(delta: float) -> void:
    if Input.is_action_pressed("jump"):
        input_buffer = true
    else:
        input_buffer = false

func _physics_process(delta: float) -> void:
    velocity.y += gravity * delta
    if Input.is_action_pressed("ui_right"):
        velocity.x = speed
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -speed
    else:
        velocity.x = lerp(velocity.x, 0, 0.1)

    # 跳跃逻辑(包括缓冲)
    if (Input.is_action_just_pressed("jump") or input_buffer) and is_on_floor():
        velocity.y = jump_force
        jumped.emit()
        input_buffer = false

    move_and_slide(velocity, Vector2.UP)

    if is_on_floor():
        print("站在地面上")
    else:
        print("空中")

预期输出:角色可以在按键稍晚时依然触发跳跃,并且不会因为按键延迟而漏跳。

验证与踩坑

测试时可使用以下命令启动游戏并检查行为是否符合预期:

godot --project-path /home/user/projects/godot_jump_test --fullscreen

预期输出:游戏窗口以全屏模式运行,角色能够正常移动和跳跃。

如果出现以下报错:

Error: Cannot call 'lerp' with argument 'to' of type 'float'. Expected type 'Vector2'.

解决办法是将 lerp(velocity.x, 0, 0.1) 替换为 lerp(Vector2(velocity.x, 0), Vector2.ZERO, 0.1).x,确保参数类型匹配。排查步骤:在 Godot 编辑器中打开脚本文件,点击错误提示的行号查看详细信息;若仍无法解决,尝试添加一个 print(type(velocity.x)) 来确认变量类型是否为期望值。

小结

  • 使用 move_and_slide() 和 is_on_floor() 正确管理角色的物理状态;
  • 调整重力值和跳跃初速度使手感更真实;
  • 增加土狼时间实现跳跃缓冲,提升体验;
  • 每帧处理中避免重复乘以 delta 导致的手感变化问题。