2022-11-27 13:31:09 -07:00
|
|
|
extends Spatial
|
|
|
|
|
|
|
|
var yaw_pitch = Vector2.ZERO
|
|
|
|
var mouse_look_sensitivity = 0.1
|
2023-01-04 20:04:33 -07:00
|
|
|
var controller_look_sensitivity = 2.3
|
2022-11-27 13:31:09 -07:00
|
|
|
var target_yaw_pitch = Vector2.ZERO
|
|
|
|
var look_acceleration = 0.4
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
|
|
|
|
func rotate_camera(relative : Vector2):
|
|
|
|
target_yaw_pitch -= relative*PI/180.0
|
|
|
|
target_yaw_pitch.x = wrapf(target_yaw_pitch.x, -PI, PI)
|
|
|
|
target_yaw_pitch.y = clamp(target_yaw_pitch.y, -PI/2.0, PI/2.0)
|
|
|
|
|
|
|
|
func _input(event):
|
|
|
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
|
|
|
if event is InputEventMouseMotion:
|
|
|
|
rotate_camera(event.relative * mouse_look_sensitivity)
|
|
|
|
if Input.is_action_just_pressed("ui_cancel"):
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
else:
|
2023-01-04 19:25:46 -07:00
|
|
|
if event is InputEventMouseButton and Input.is_action_just_pressed("ui_cancel"):
|
2022-11-27 13:31:09 -07:00
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
2023-01-04 20:04:33 -07:00
|
|
|
|
|
|
|
|
|
|
|
func controller_input():
|
2022-11-27 13:31:09 -07:00
|
|
|
var look_relative = Vector2(
|
|
|
|
Input.get_action_strength("player_look_right") - Input.get_action_strength("player_look_left"),
|
|
|
|
Input.get_action_strength("player_look_down") - Input.get_action_strength("player_look_up")
|
|
|
|
)
|
|
|
|
rotate_camera(look_relative * controller_look_sensitivity)
|
|
|
|
|
|
|
|
func _process(_delta):
|
2023-01-04 20:04:33 -07:00
|
|
|
controller_input()
|
2022-11-27 13:31:09 -07:00
|
|
|
yaw_pitch.x = wrapf(lerp_angle(yaw_pitch.x, target_yaw_pitch.x, look_acceleration), -PI, PI)
|
|
|
|
yaw_pitch.y = lerp_angle(yaw_pitch.y, target_yaw_pitch.y, look_acceleration)
|
|
|
|
|
|
|
|
rotation = Vector3.ZERO
|
|
|
|
rotate_y(yaw_pitch.x)
|
|
|
|
|
|
|
|
$Rotation.rotation = Vector3.ZERO
|
|
|
|
$Rotation.rotate_x(yaw_pitch.y)
|