How to make mouse motion lean the first person character a certain distance?

Godot Version

v4.2.2.stable.official [15073afe3]


Hello. I currently have a game where the player is locked facing an enemy and can use the wasd keys to move toward, away, and around said enemy. I would like to have my mouse movements lean the camera left and right. This is what I currently have:

func _input(event):
	if event is InputEventMouseMotion:
		rotate(global_transform.basis.z.normalized(), -event.relative.x * 0.001)

The problem is, that there is no limit to how far the camera can rotate. Also, when the player moves around the enemy to face a different direction, the camera becomes all wonky with a gimbal-like effect. I would appreciate any help I can get with this issue. Thanks!

You need to specify a rotation or angle limit. There are lots of ways to do this.

Your other problem is called gimbal lock.

Here is a resource explaining it. It may also have solutions.