meat_madness_redux/godot/player/CameraController.gd

46 lines
1.5 KiB
GDScript

extends Spatial
var yaw_pitch = Vector2.ZERO
var mouse_look_sensitivity = 0.1
var controller_look_sensitivity = 2.3
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:
if event is InputEventMouseButton and Input.is_action_just_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func controller_input():
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):
controller_input()
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)