extends Node @export_category("Camera") @export var LOOK_SENSITIVITY = 1.0 ##camera sensitivity @export var CAMERA_VELOCITY_MULTIPLIER = 1.5 ##how heavy should the UI movement feel @export var CAMERA_BOB_FREQUENCY = 2.4 ##how quick the camera bobs whilst moving @export var CAMERA_BOB_AMPLITUDE = 0.08 ##how hard the camera bobs whilst moving @export var CAMERA_BOB_MULTIPLIER = 200.0 ##how much the ui reacts to head bobbing @export var CAMERA_SIDEWAYS_BOB_AMPLITUDE = 0.03 ## how many degrees does the camera bob sideways @export_category("UI") @export var UI_SCALE: float = 1 ##rational scale of the ui @export var UI_MOVEMENT_SENSITIVITY = 0.5 ##how much the UI moves when you move the camera @export var UI_MOVEMENT_SPEED = 15.0 ##how fast the UI moves when you move the camera @export var UI_MOVEMENT_VELOCITY_MULTIPLIER = 2.0 ##how much the ui leans when walking or strafing @onready var player: PlayerCharacter = $"../.." @onready var sound_controller: Node = %SoundController @onready var camera: Camera3D = %PlayerCamera @onready var ui: Control = %PlayerUi @onready var player_properties: Node = %PlayerProperties @onready var ui_base_position = ui.position @onready var ui_handler: Node = %UiHandler @onready var player_state: Node = %PlayerState @onready var input_controller: Node = %InputController var t_bob: float = 0.0 var last_bob_y: float = 0.0 var t_breath: float = 0.0 var last_breath_y: float = 0.0 var _camera_original_position: Vector3 = Vector3(0,.75,0) var _last_camera_global_position: Vector3 = Vector3.ZERO var _breathing: bool = true func _ready() -> void: _last_camera_global_position = camera.global_position func _process(delta: float) -> void: if player.is_on_floor(): _breathing = true else: _breathing = false footstep_sound() breathing_sound() func do(delta): ui.offset_transform_scale *= UI_SCALE ui_movement_handling(delta) ui_handler.set_stamina_bar(player_properties.stamina) var horizontal_speed = Vector3(player.velocity.x, player.velocity.y, player.velocity.z).length() if _breathing: t_breath += delta * (100 / (40 + player_properties.stamina)) var target_pos = get_breathing_bob_offset_pos() var target_rot = get_breathing_bob_offset_rot() if player.is_on_floor() and horizontal_speed > 0.05: t_bob += delta * (player.velocity.length() * float(player.is_on_floor()) + (int(Input.is_action_pressed("crouch"))) * 3) target_pos += get_moving_bob_offset_pos() target_rot += get_moving_bob_offset_rot() camera.transform.origin = camera.transform.origin.lerp(target_pos, delta * 10) var final_pitch = camera.rotation.x + target_rot.x var final_roll = target_rot.z var target_basis = Basis.from_euler(Vector3(final_pitch, 0, final_roll)) camera.transform.basis = camera.transform.basis.slerp(target_basis, delta * 5) func ui_movement_handling(delta: float): var mouse_offset = -input_controller.mouse_motion_delta * UI_MOVEMENT_SENSITIVITY var current_camera_global_position = camera.global_position var camera_global_velocity = (current_camera_global_position - _last_camera_global_position) / delta var cam_local_vel = camera.global_transform.basis.inverse() * camera_global_velocity var inertia_offset = Vector2( -cam_local_vel.x, cam_local_vel.y - (cam_local_vel.z * 0.25) ) * CAMERA_VELOCITY_MULTIPLIER var target_offset = mouse_offset + inertia_offset ui.position = ui.position.lerp(ui_base_position + target_offset, delta * UI_MOVEMENT_SPEED) _last_camera_global_position = current_camera_global_position var ui_scaling_target = Vector2(-cam_local_vel.z * UI_MOVEMENT_SENSITIVITY * .002 + 1, -cam_local_vel.z * UI_MOVEMENT_SENSITIVITY * .002 + 1) ui.offset_transform_scale = ui.offset_transform_scale.lerp(ui_scaling_target, delta * UI_MOVEMENT_SPEED) var ui_rotation_target = -inertia_offset.x - mouse_offset.x * 0.25 ui.offset_transform_rotation = lerpf(ui.offset_transform_rotation, ui_rotation_target * UI_MOVEMENT_SENSITIVITY * 0.003, delta * UI_MOVEMENT_SPEED) func footstep_sound(): var current_sin_pos = sin(t_bob * CAMERA_BOB_FREQUENCY) if last_bob_y >= -.95 and current_sin_pos < -.95: sound_controller.play_footstep_sound() last_bob_y = current_sin_pos func breathing_sound(): if _breathing: var current_sin_pos = sin(t_breath * CAMERA_BOB_FREQUENCY) if last_breath_y <= -.60 and current_sin_pos > -.60: sound_controller.play_breath_sound(true) if last_breath_y >= .60 and current_sin_pos < .60: sound_controller.play_breath_sound(false) last_breath_y = current_sin_pos func get_breathing_bob_offset_pos() -> Vector3: return Vector3(0, sin(t_breath * CAMERA_BOB_FREQUENCY) * CAMERA_BOB_AMPLITUDE + .75, 0) func get_breathing_bob_offset_rot() -> Vector3: return Vector3(sin(t_breath * CAMERA_BOB_FREQUENCY) * CAMERA_SIDEWAYS_BOB_AMPLITUDE, 0, 0) func get_moving_bob_offset_pos() -> Vector3: return Vector3(0, sin(t_bob * CAMERA_BOB_FREQUENCY) * CAMERA_BOB_AMPLITUDE, 0) func get_moving_bob_offset_rot() -> Vector3: return Vector3(0, 0, sin(t_bob * CAMERA_BOB_FREQUENCY * .5) * CAMERA_SIDEWAYS_BOB_AMPLITUDE)