Need help with working on a pause screen

Godot Version

4.7.2

Question

I’m working on a pause screen for my game, and have managed to get it to toggle on and off with the escape key. However, the tutorial I followed did not include instructions on how to use the mouse, and since the pause screen is a canvas overlay, I have concluded that the player’s camera code is causing the mouse to be stuck in the middle of the screen.

The code for said camera is in a script attached to my player, but I don’t know how I could stop the mouse from being fixed to the center while the pause overlay is there. Maybe I’m just dumb, but if anyone has any pointers, I’d greatly appreciate it.

extends Node3D

var sensitivity = 0.2

@onready var torch: SpotLight3D = $Camera3D/Torch

func _ready() -> void:
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event: InputEvent) -> void:
	# Get mouse motion and handle the camera's rotation.
	if event is InputEventMouseMotion:
		get_parent().rotate_y(deg_to_rad(-event.relative.x * sensitivity))
		rotate_x(deg_to_rad(-event.relative.y * sensitivity))
		rotation.x = clamp(rotation.x, deg_to_rad(-90), deg_to_rad(90))
	
	# Toggle the torch on and off.
	if event.is_action_pressed("torch"):
		torch.visible = not torch.visible

this given code is problem when we set the mouse mode captured it locked down to the center. you simply change the mouse_mode from captured to anything else like confined. This solve your problem.

use this whenever you want to move your mouse to move freely
Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)

OK, I added those mouse mode lines and that fixed the problem, but now I can actually interact with the menu, I’ve noticed two more bugs:

  1. Pressing the escape key (which I set as the pause input) resets the mouse to the middle of the screen instead of unpausing the game, which it did before implementing your fix.
  2. Upon pressing the button to return to the title screen scene, I am unable to click any of the buttons in it.

Here is my menu code if it makes things any easier.

extends CanvasLayer

func _ready() -> void:
	visible = false
	get_tree().paused = false

func _input(_event: InputEvent) -> void:
	if Input.is_action_just_pressed("pause"):
		if get_tree().paused:
			visible = false
			get_tree().paused = false
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		else:
			visible = true
			get_tree().paused = true
			Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)

func _on_resume_pressed() -> void:
	visible = false
	get_tree().paused = false
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _on_settings_pressed() -> void:
	pass # Replace with function body.

func _on_controls_pressed() -> void:
	pass # Replace with function body.

func _on_menu_quit_pressed() -> void:
	get_tree().change_scene_to_file("res://Scenes/Rooms/title_screen.tscn")

func _on_game_quit_pressed() -> void:
	get_tree().quit()

func _input should make use of the provided event instead of the global Input, and unhandled input is typically better too.

func _unhandled_input(_event: InputEvent) -> void:
	if event.is_action_pressed("pause"):

Make sure you also do not have any other scripts handling pausing.

You game may still be paused in the title screen

Thank you so much, both of these worked!