grounders-slowjam-2024/camera/camera.gd

36 lines
917 B
GDScript3
Raw Normal View History

2024-05-25 10:10:24 -06:00
extends Node3D
const MOUSE_SENSITIVITY := .001
2024-05-25 12:07:16 -06:00
const MOUSE_VISCOSITY := 0.5
var _goal_rotation_y := rotation.y
2024-05-25 10:10:24 -06:00
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)
2024-05-25 12:07:16 -06:00
var rot_y := lerp_angle(rotation.y, _goal_rotation_y, MOUSE_VISCOSITY)
rotation.y = rot_y
2024-05-25 10:10:24 -06:00
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
2024-05-25 12:07:16 -06:00
_goal_rotation_y -= move.x
2024-05-25 10:10:24 -06:00
func _input(event):
if event is InputEventMouseMotion:
handle_camera_movement(event.relative)