人物控制器1

This commit is contained in:
2026-10-09 15:51:24 +08:00
parent d20d45fffc
commit 96e21757c3
6 changed files with 140 additions and 25 deletions
+29 -12
View File
@@ -9,18 +9,29 @@ extends Node
const JUMP_BUFFER_TIME: float = 0.1
@export_group("移动")
## 常规移动速度(米/秒)
@export var walk_speed: float = 5.0
## 疾跑速度(米/秒)
@export var sprint_speed: float = 8.5
## 地面加速度(米/秒²)
@export var acceleration: float = 45.0
## 【设计基准】以人类为参照做等比缩放:人 1.75 m,游戏惯例 walk 5.0 / sprint 8.5 m/s。
## 本角色身高 0.15 m,系数 k = 0.15 / 1.75 ≈ 0.0857,下面所有线性量都是原值 × k。
## 这样「每秒走过几个自身体长」和人类一致,动作比例看起来才对。
##
## 换尺寸时的公式:线性量(速度/加速度)× k',跳跃初速度 × √k'(因为跳跃高度 = v²/2g)。
##
## 常规移动速度(米/秒)—— 约 2.9 身长/秒,和人类走路同一比例
@export var walk_speed: float = 0.4286
## 疾跑速度(米/秒)—— 约 4.9 身长/秒
@export var sprint_speed: float = 0.7286
## 蹲行速度(米/秒)—— 默认是走速的一半。蹲下时按住 Shift 也不会疾跑。
## 注意:改这个值要同步改 player_animator.gd 的 sneak_ref_speed,否则蹲走时脚步会打滑。
@export var crouch_speed: float = 0.2143
## 地面加速度(米/秒²)—— 约 0.11 秒加到全速,起步干脆
@export var acceleration: float = 3.8571
## 空中加速度(米/秒²)
@export var air_acceleration: float = 12.0
## 起跳初速度(米/秒)
@export var jump_velocity: float = 5.0
@export var air_acceleration: float = 1.0286
## 起跳初速度(米/秒)—— 跳跃高度 ≈ 0.109 m,约 0.73 身高
## 注意是 ×√k 而不是 ×k:跳跃高度 = v² / 2g,想让高度也等比缩,速度就得开方。
@export var jump_velocity: float = 1.4639
## 本帧是否在疾跑 / 是否按住蹲键。Player 读这两个值转交给 PlayerAnimator。
## 本帧是否在疾跑 / 是否按住蹲键。
## Player 读这两个值转交给 PlayerAnimator 切片段,crouching 还用来压低摄像机。
var sprinting: bool = false
var crouching: bool = false
@@ -72,9 +83,15 @@ func step(delta: float) -> void:
wish = wish.normalized()
# 只有真正按了方向键才允许疾跑(避免原地"疾跑"触发跑动动画)
sprinting = Input.is_action_pressed("sprint") and input_dir.length_squared() > 0.000001
var wants_sprint := Input.is_action_pressed("sprint") and input_dir.length_squared() > 0.000001
crouching = Input.is_action_pressed("crouch")
var target_speed := sprint_speed if sprinting else walk_speed
# 蹲伏优先于疾跑:蹲着时按住 Shift 不算疾跑,速度取蹲行速度。
sprinting = wants_sprint and not crouching
var target_speed := walk_speed
if crouching:
target_speed = crouch_speed
elif sprinting:
target_speed = sprint_speed
var accel := acceleration if _body.is_on_floor() else air_acceleration
var horizontal := Vector2(_body.velocity.x, _body.velocity.z)