49 lines
1.8 KiB
GDScript
49 lines
1.8 KiB
GDScript
extends CharacterBody3D
|
|
## First-person player controller — M0.
|
|
##
|
|
## Move (WASD), look (mouse), jump (space). Gravity + ground collision via
|
|
## CharacterBody3D. Deliberately minimal: no stamina/health yet (M1), no network
|
|
## ownership yet (M2). Press ESC to free the mouse, click to recapture.
|
|
|
|
@export var speed: float = 5.0
|
|
@export var sprint_speed: float = 8.0
|
|
@export var jump_velocity: float = 5.5
|
|
@export var mouse_sensitivity: float = 0.0025
|
|
|
|
@onready var camera: Camera3D = $Camera3D
|
|
|
|
var _gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity", 9.8)
|
|
|
|
func _ready() -> void:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
|
rotate_y(-event.relative.x * mouse_sensitivity)
|
|
camera.rotate_x(-event.relative.y * mouse_sensitivity)
|
|
camera.rotation.x = clamp(camera.rotation.x, -1.4, 1.4)
|
|
elif event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE:
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
elif event is InputEventMouseButton and event.pressed:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if not is_on_floor():
|
|
velocity.y -= _gravity * delta
|
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
velocity.y = jump_velocity
|
|
|
|
var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_back")
|
|
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
|
|
var current_speed := sprint_speed if Input.is_key_pressed(KEY_SHIFT) else speed
|
|
if direction:
|
|
velocity.x = direction.x * current_speed
|
|
velocity.z = direction.z * current_speed
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, current_speed)
|
|
velocity.z = move_toward(velocity.z, 0, current_speed)
|
|
|
|
move_and_slide()
|