How to keep mouse still or from warping to the middle?

You don’t need the _process(), you can do it all in the _unhandled_input(), should be more efficient this way.

I haven’t changed any of the main logic and this code works for me no matter if I move the mouse or let it be still. Does it not work for you? What happens for you?

extends Node3D


@export_range(0.0, 1.0) var mouse_sensitivity = 0.01
@export var tilt_limit = deg_to_rad(80)
var rotating = false
var old_mouse_position: Vector2i


func _unhandled_input(event : InputEvent) -> void:
	if event.is_action_pressed("rotate camera"):
		rotating = true
		old_mouse_position = get_viewport().get_mouse_position()
		Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	elif event.is_action_released("rotate camera"):
		rotating = false
		Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		DisplayServer.warp_mouse(old_mouse_position)
	elif event is InputEventMouseMotion and rotating:
		rotation.y -= event.relative.x * mouse_sensitivity
		rotation.x -= event.relative.y * mouse_sensitivity
		rotation.x = clampf(rotation.x, -tilt_limit, tilt_limit)