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

Godot 3D游戏定向空中冲刺机制失效问题求助

解决Godot 3D CharacterBody3D定向空中冲刺失效问题

问题根源分析

你的冲刺功能失效核心原因如下:

  • double_jump_air_dash仅输出日志,未实际修改角色速度
  • _physics_process中会用普通移动速度覆盖冲刺速度,导致冲刺效果被抵消
  • 未添加冲刺状态控制,无法限制冲刺时长、防止连续触发
  • 方向计算未基于玩家本地坐标系,可能导致冲刺方向与视角不符

修改后的完整代码

extends CharacterBody3D
# 可导出的玩家参数
@export var player_speed = 12.0
@export var fall_acceleration = 60
@export var jump_velocity = 20
@export var dash_velocity = 30  # 调高冲刺速度让效果更明显
@export var dash_duration = 0.2  # 控制冲刺持续时间

@onready var jumpcd_time = $JumpCd_Timer
@onready var dash_time = $DashTimer

var target_velocity = Vector3.ZERO
var jumpCD_On = false
var can_jumpDash = false
var is_dashing = false  # 新增冲刺状态标记

func _ready():
    # 一次性初始化计时器参数,避免重复设置
    jumpcd_time.wait_time = 3
    jumpcd_time.one_shot = true
    dash_time.wait_time = dash_duration
    dash_time.one_shot = true
    dash_time.timeout.connect(_end_dash)

func jump():
    # 重置CD状态
    if jumpcd_time.is_stopped():
        jumpCD_On = false

    # 地面跳跃逻辑
    if is_on_floor() and not jumpCD_On and Input.is_action_just_pressed("jump"):
        can_jumpDash = true
        target_velocity.y = jump_velocity
        jumpcd_time.start()
        jumpCD_On = true
    # 空中二段跳/冲刺逻辑
    elif not is_on_floor() and can_jumpDash and Input.is_action_just_pressed("jump"):
        double_jump_air_dash()
        can_jumpDash = false

func double_jump_air_dash():
    var dash_dir = Vector3.ZERO
    # 收集冲刺方向输入
    if Input.is_action_pressed("move_forward"):
        dash_dir.x += 1
    elif Input.is_action_pressed("move_backward"):  # 修正拼写错误
        dash_dir.x -= 1
    if Input.is_action_pressed("move_left"):
        dash_dir.z -= 1
    elif Input.is_action_pressed("move_right"):
        dash_dir.z += 1

    if dash_dir != Vector3.ZERO:
        # 基于玩家本地坐标系计算冲刺方向
        dash_dir = dash_dir.normalized()
        dash_dir = $PlayerDirection.basis * dash_dir
        # 设置冲刺速度
        target_velocity.x = dash_dir.x * dash_velocity
        target_velocity.z = dash_dir.z * dash_velocity
        target_velocity.y = 0  # 冲刺时重置垂直速度,可根据需求调整
        is_dashing = true
        dash_time.start()
        print("定向冲刺")
    else:
        # 无方向输入时触发二段跳
        target_velocity.y = jump_velocity
        print("二段跳")

func normalize_speed(direction):
    if direction != Vector3.ZERO:
        direction = direction.normalized()
        $PlayerDirection.basis = Basis.looking_at(direction)

func _end_dash():
    # 结束冲刺状态
    is_dashing = false

func _physics_process(delta):
    var direction = Vector3.ZERO

    # 非冲刺状态下才处理普通移动输入
    if not is_dashing:
        if Input.is_action_pressed("move_forward"):
            direction.x += 1
        if Input.is_action_pressed("move_backward"):
            direction.x -= 1
        if Input.is_action_pressed("move_left"):
            direction.z -= 1
        if Input.is_action_pressed("move_right"):
            direction.z += 1

        normalize_speed(direction)
        # 更新普通移动速度
        target_velocity.x = direction.x * player_speed
        target_velocity.z = direction.z * player_speed

    # 重力处理
    if not is_on_floor():
        target_velocity.y -= fall_acceleration * delta

    # 应用速度并移动
    velocity = target_velocity
    move_and_slide()

关键改动说明

  1. 冲刺状态控制:新增is_dashing变量和_end_dash回调,通过计时器限制冲刺时长,避免普通移动覆盖冲刺速度
  2. 生效的冲刺速度:在double_jump_air_dash中计算玩家本地方向的冲刺向量,并赋值给target_velocity
  3. 优化计时器初始化:在_ready中一次性设置参数,避免重复操作
  4. 修正输入拼写:将move_backword改为move_backward,避免输入检测失效
  5. 移动与冲刺互斥:冲刺时跳过普通移动输入处理,确保冲刺效果不被抵消

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:23:27