grounders-slowjam-2024/camera/camera.gd

36 lines
917 B
GDScript

extends Node3D
const MOUSE_SENSITIVITY := .001
const MOUSE_VISCOSITY := 0.5
var _goal_rotation_y := rotation.y
func get_input_direction() -> Vector2:
var input := Input.get_vector("move_left", "move_right", "move_up", "move_down")
if input == Vector2.ZERO:
return Vector2.ZERO
var dir := input.normalized()
input = input * dir.abs()
return -1.0 * dir.rotated(-rotation.y + PI)
func _process(_delta):
get_parent().movement_dir = get_input_direction()
%SpringArm3D.look_at(global_position)
var rot_y := lerp_angle(rotation.y, _goal_rotation_y, MOUSE_VISCOSITY)
rotation.y = rot_y
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func handle_camera_movement(move: Vector2):
if Input.mouse_mode != Input.MOUSE_MODE_CAPTURED:
return
move *= MOUSE_SENSITIVITY
_goal_rotation_y -= move.x
func _input(event):
if event is InputEventMouseMotion:
handle_camera_movement(event.relative)