camera: made mouse control camera

This commit is contained in:
Simon Ward 2026-01-23 09:16:23 +13:00 committed by Jeremy Baxter
parent 7c700fb288
commit d4b571428b
3 changed files with 44 additions and 12 deletions

View file

@ -1,6 +1,12 @@
class_name CameraGimbal extends Node3D
@export var rotation_speed = (PI / 180) * 120
@export var shift_lock = false
var locked_mouse_positon = Vector2(0,0)
var mouse_locked = false
var mouse_x_velocity = 0
var mouse_y_velocity = 0
func reset():
basis = Basis()
@ -13,21 +19,38 @@ func rotate_camera_x(x, delta):
Vector3.RIGHT, x * rotation_speed * delta)
$InnerGimbal.rotation.x = clamp($InnerGimbal.rotation.x,-0.8,0.7)
func _input(event):
if event is InputEventMouseMotion:
mouse_x_velocity = event.velocity.x
mouse_y_velocity = event.velocity.y
func _process(delta):
# rotate outer gimbal around y axis
var y = 0
if Input.is_action_pressed("cam_left"):
y += 1
if Input.is_action_pressed("cam_right"):
y -= 1
rotate_camera_y(y, delta)
# rotate inner gimbal around x axis
var x = 0
if Input.is_action_pressed("cam_up"):
x -= 1
if Input.is_action_pressed("cam_down"):
x += 1
if Input.is_action_just_pressed("shift_lock"):
if shift_lock == true:
shift_lock = false
else:
shift_lock = true
elif Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) or shift_lock == true:
if mouse_locked == false:
mouse_locked = true
locked_mouse_positon = get_viewport().get_mouse_position()
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if mouse_x_velocity > 200 or mouse_x_velocity < -200:
y -= clamp(mouse_x_velocity / 500, -5, 5)
if mouse_y_velocity > 200 or mouse_y_velocity < -200:
x -= clamp(mouse_y_velocity / 1000, -5, 5)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
mouse_locked = false
mouse_x_velocity = 0
mouse_y_velocity = 0
rotate_camera_y(y, delta)
rotate_camera_x(x, delta)
# reset gimbals