Mouse isn't captured

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

i made a camera script which captures the mouse and rotates the head for movement it used to work but now suddenly doesnt
script is

extends Node3D

var sens = 0.005

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		get_parent().rotate_y(-event.relative.x*sens)
		rotate_x(-event.relative.y*sens)
		rotation.x=clamp(rotation.x, deg_to_rad(-75, deg_to_rad(60))

Try setting up a button that can toggle mousemode on or off.

My guess is either the ready function happens at an bad time now. Or there is some other code after that ready function changing the capture mode. Or something happening to the OS.

func _unhandled_input(event: InputEvent): 
	if event is InputEventMouseButton:
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	elif event.is_action_pressed("ui_cancel"):
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
#just put your camera movement stuff here and you should be good to go

tried this doesn’t work for me

did you add a canvas layer, or something to with a hud?

sorry not to hijack OP’s post, but I added a canvas layer. does that prevent it from working? do I need to do something to get around it?

canvas layer by default have mouse interaction on, so it can prevent mouse capturing if you are using an unhandled input event to detect the mouse
I had learn this the hard way
sorry for the late reply