2 class_name PlayerCameraHandler
5 @export_range(1.0, 10.0) var camera_distance := 3.0
7 @export_range(0.0, 1.0) var mouse_sensitivity := 0.2
8 @export_range(0.0, 1.0) var mouse_sensitivity_x := 1.0
9 @export_range(0.0, 1.0) var mouse_sensitivity_y := mouse_sensitivity_x / 2
11 @export_range(0.0, 1.0) var joystick_sensitivity := 0.15
12 @export_range(0.0, 10.0) var joystick_sensitivity_x := 4.0
13 @export_range(0.0, 10.0) var joystick_sensitivity_y := joystick_sensitivity_x / 2
16 @onready var spring: SpringArm3D = $SpringArm3D
17 @onready var camera: Camera3D = $SpringArm3D/Camera3D
20 # Called when the node enters the scene tree for the first time.
21 func _ready() -> void:
22 spring.spring_length = camera_distance
25 # Called every frame. 'delta' is the elapsed time since the previous frame.
26 func _process(delta: float) -> void:
27 spring.spring_length = lerp(
28 spring.spring_length, camera_distance, delta
32 func update(input: InputPacket, delta: float):
33 var camera_input_direction := input.camera_input_direction
35 if input.camera_input_method == CameraInput.MOUSE:
36 camera_input_direction *= Vector2(
37 mouse_sensitivity_x, -mouse_sensitivity_y
39 elif input.camera_input_method == CameraInput.JOYSTICK:
40 camera_input_direction *= Vector2(
41 joystick_sensitivity_x, -joystick_sensitivity_y
42 ) * joystick_sensitivity
44 # vertical camera rotation
45 rotation.x += camera_input_direction.y * delta
46 rotation.x = clamp(rotation.x, -PI / 6, PI / 3)
48 # horizontal camera rotation
49 rotation.y -= camera_input_direction.x * delta
52 # Get the XZ input direction based on player's input relative to the camera
53 func get_xz_direction_relative_to_camera(d: Vector2) -> Vector2:
55 var forward := camera.global_basis.z
56 var right := camera.global_basis.x
57 var dir3 := forward * d.y + right * d.x
58 return Vector2(dir3.x, dir3.z).normalized()