How do i recapture the mouse once i make it visible?

Godot Version

Godot 4

Question

Hi, im new
I was trying to recapture the cursor once is out of the camera of the player

This is the code

func _process(delta):
if Input.is_action_just_pressed(“ui_cancel”):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.MOUSE_MODE_VISIBLE:
Input.is_action_just_pressed(“ui_cancel”)
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

I dont know if i need to do or add something else or if i have to map a new key to make it work properly

Just change it to:

if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:

As a short version of your codes, try this:

if Input.is_action_just_pressed("ui_cancel"):
    if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
    else:
        Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
1 Like