How to fix my x axis camera clamp?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Brad4x

I coded the recoil of the camera to rotate on the x axis but I’m running into an issue. After shooting for a short time the rotation of the clamp changes and I cannot look fully down and too far behind the player. I believe the recoil camera rotation is messing it up. Do you guys have any possible solution for something like this? Thanks in advance

https://youtu.be/WdzifU4b8-A

func _input(event):

if event is InputEventMouseMotion:

rotate\_y(deg2rad(-event.relative.x \* mouse\_sens))

head.rotate\_x(deg2rad(-event.relative.y \* mouse\_sens))

head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))

func fire():

if Input.is_action_pressed(“fire”):

var recoil = camera.rotate\_object\_local(Vector3(1,0,0),0.005)

recoil = get\_global\_transform().basis.x

For future reference, this question was cross-posted on Reddit: Reddit - Dive into anything

Calinou | 2020-12-28 02:00

:bust_in_silhouette: Reply From: Calinou

(Cross-posting my answer from Reddit for posterity.)

It sounds like you’re a victim of gimbal lock. To solve this in a durable way, you will have to think in 3D transforms instead of using angles. Using 3D transforms is more difficult at first, but the price is well worth it as soon as you need to rotate on more than one axis.

See Using 3D transforms in the documentation for more information.