Files
demo-godot/palyer.gd

35 lines
910 B
GDScript3
Raw Permalink Normal View History

2026-01-04 19:35:08 +08:00
extends CharacterBody3D
2026-01-07 23:54:15 +08:00
var moveUtils = MoveUtils.new()
2026-01-04 19:35:08 +08:00
var fall_acceleration = 75
var target_velocity = Vector3.ZERO
2026-01-06 00:18:13 +08:00
func _physics_process(delta: float) -> void:
2026-01-07 23:54:15 +08:00
target_velocity = moveUtils.get_horizontal_velocity(get_cmd_direction(), velocity, delta)
2026-01-07 13:06:12 +08:00
if target_velocity != Vector3.ZERO:
$pivot.basis = Basis.looking_at(target_velocity.normalized())
2026-01-07 23:54:15 +08:00
2026-01-04 19:35:08 +08:00
if !is_on_floor():
target_velocity.y = target_velocity.y - ( fall_acceleration * delta )
velocity = target_velocity
move_and_slide()
2026-01-06 00:18:13 +08:00
## 获取当前指令的关联向量[br]
## 难道需要两层才生效?
2026-01-07 13:06:12 +08:00
func get_cmd_direction() -> Vector3:
2026-01-06 00:18:13 +08:00
var vector := Vector3.ZERO
if Input.is_action_pressed("move_forward"):
vector.z -= 1
if Input.is_action_pressed("move_back"):
vector.z += 1
if Input.is_action_pressed("move_left"):
vector.x -= 1
if Input.is_action_pressed("move_right"):
vector.x += 1
return vector.normalized()