Files
Trinittu/scripts/test_camera.gd
Overside srl b158a646c3 revert
2026-02-17 08:37:55 +01:00

70 lines
2.2 KiB
GDScript

## Free-look camera controller for the test scene.
## WASD to move, mouse drag (right-click) to look around, scroll to change speed.
extends Camera3D
@export_group("Movement")
@export var move_speed: float = 20.0
@export var fast_multiplier: float = 3.0
@export var smooth_factor: float = 10.0
@export_group("Mouse Look")
@export var mouse_sensitivity: float = 0.003
@export var pitch_limit: float = 89.0
var _yaw: float = 0.0
var _pitch: float = 0.0
var _looking: bool = false
var _velocity: Vector3 = Vector3.ZERO
func _ready() -> void:
var euler := rotation
_yaw = euler.y
_pitch = euler.x
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
var mb := event as InputEventMouseButton
if mb.button_index == MOUSE_BUTTON_RIGHT:
_looking = mb.pressed
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if _looking else Input.MOUSE_MODE_VISIBLE
if mb.button_index == MOUSE_BUTTON_WHEEL_UP:
move_speed = minf(move_speed * 1.2, 500.0)
if mb.button_index == MOUSE_BUTTON_WHEEL_DOWN:
move_speed = maxf(move_speed / 1.2, 1.0)
if event is InputEventMouseMotion and _looking:
var motion := event as InputEventMouseMotion
_yaw -= motion.relative.x * mouse_sensitivity
_pitch -= motion.relative.y * mouse_sensitivity
_pitch = clampf(_pitch, deg_to_rad(-pitch_limit), deg_to_rad(pitch_limit))
func _process(delta: float) -> void:
rotation = Vector3(_pitch, _yaw, 0.0)
var input_dir := Vector3.ZERO
if Input.is_physical_key_pressed(KEY_W):
input_dir.z -= 1.0
if Input.is_physical_key_pressed(KEY_S):
input_dir.z += 1.0
if Input.is_physical_key_pressed(KEY_A):
input_dir.x -= 1.0
if Input.is_physical_key_pressed(KEY_D):
input_dir.x += 1.0
if Input.is_physical_key_pressed(KEY_E) or Input.is_physical_key_pressed(KEY_SPACE):
input_dir.y += 1.0
if Input.is_physical_key_pressed(KEY_Q) or Input.is_physical_key_pressed(KEY_CTRL):
input_dir.y -= 1.0
input_dir = input_dir.normalized()
var speed := move_speed
if Input.is_physical_key_pressed(KEY_SHIFT):
speed *= fast_multiplier
var target_velocity := (global_transform.basis * input_dir) * speed
_velocity = _velocity.lerp(target_velocity, clampf(smooth_factor * delta, 0.0, 1.0))
global_position += _velocity * delta