Files
tgcc/docs/museums/daynight/camera_3d.gd

71 lines
2.4 KiB
GDScript

extends Camera3D
@export_group("Movement")
@export var move_speed: float = 10.0
@export var fast_move_multiplier: float = 3.0
@export var move_smoothing: float = 10.0
@export_group("Look")
@export var mouse_sensitivity: float = 0.3
@export var invert_y: bool = false
var _velocity: Vector3 = Vector3.ZERO
var _yaw: float = 0.0 # Y rotation
var _pitch: float = 0.0 # X rotation
var _is_active: bool = false
func _ready() -> void:
#Current rotation
_yaw = rotation_degrees.y
_pitch = rotation_degrees.x
func _unhandled_input(event: InputEvent) -> void:
#Activate camera movement using mouse right button
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_RIGHT:
_is_active = event.pressed
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if _is_active else Input.MOUSE_MODE_VISIBLE
if event is InputEventMouseMotion and _is_active:
_yaw -= event.relative.x * mouse_sensitivity
var pitch_delta = event.relative.y * mouse_sensitivity
_pitch += pitch_delta if invert_y else -pitch_delta
_pitch = clamp(_pitch, -89.0, 89.0)
if event.is_action_pressed("ui_cancel"):
_is_active = false
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
func _physics_process(delta: float) -> void:
rotation_degrees.y = _yaw
rotation_degrees.x = _pitch
if not _is_active:
_velocity = _velocity.lerp(Vector3.ZERO, move_smoothing * delta)
position += _velocity * delta
return
var input_dir := Vector3.ZERO
if Input.is_key_pressed(KEY_W) or Input.is_key_pressed(KEY_UP):
input_dir -= basis.z # avanti
if Input.is_key_pressed(KEY_S) or Input.is_key_pressed(KEY_DOWN):
input_dir += basis.z # indietro
if Input.is_key_pressed(KEY_A) or Input.is_key_pressed(KEY_LEFT):
input_dir -= basis.x # sinistra
if Input.is_key_pressed(KEY_D) or Input.is_key_pressed(KEY_RIGHT):
input_dir += basis.x # destra
if Input.is_key_pressed(KEY_E) or Input.is_key_pressed(KEY_PAGEUP):
input_dir += Vector3.UP # su (world space)
if Input.is_key_pressed(KEY_Q) or Input.is_key_pressed(KEY_PAGEDOWN):
input_dir += Vector3.DOWN # giù (world space)
if input_dir != Vector3.ZERO:
input_dir = input_dir.normalized()
var speed := move_speed * fast_move_multiplier if Input.is_key_pressed(KEY_SHIFT) else move_speed
var target_velocity := input_dir * speed
_velocity = _velocity.lerp(target_velocity, move_smoothing * delta)
position += _velocity * delta