tavurth / godot-simple-fps-camera

Simple FPS camera for Godot

Home Page:https://godotengine.org/asset-library/asset/265

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Godot 4.2 Mouse Not Clamping To Screen In Windowed Mode

C-Corgi opened this issue · comments

Code:

extends Camera3D

@onready var Player = get_parent()

# Increase this value to give a slower turn speed
const CAMERA_TURN_SPEED = 200

func _ready():
	# Tell Godot that we want to handle input
	set_process_input(true)

func look_updown_rotation(rotation = 0):
	var toReturn = self.get_rotation() + Vector3(rotation, 0, 0)
	# Here we'll clamp the vertical look to 90° up and down
	toReturn.x = clamp(toReturn.x, PI / -2, PI / 2)

	return toReturn

func look_leftright_rotation(rotation = 0):
	return Player.get_rotation() + Vector3(0, rotation, 0)

func _input(event):
	# We'll only process mouse motion events
	if not event is InputEventMouseMotion:
		return
	# We'll use the parent node "Player" to set our left-right rotation
	# This prevents us from adding the x-rotation to the y-rotation
	# which would result in a kind of flight-simulator camera
	Player.set_rotation(look_leftright_rotation(event.relative.x / -CAMERA_TURN_SPEED))
	# Now we can simply set our y-rotation for the camera, and let godot
	# handle the transformation of both together
	self.set_rotation(look_updown_rotation(event.relative.y / -CAMERA_TURN_SPEED))


func _enter_tree():
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)

func _leave_tree():
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

Note

Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN) only hides it and doesnt clamp it to the screen

Fix:

func _enter_tree():
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

Fixed in #10