Gimbal lock/ CFrame problem

So I want to “stabilize” the camera of the player to the horizon with 30 degrees of freedom, if Y is above or below that it wont stabilize anymore.

local PartRotation = CurrentCamera.Rotation

local rx,ry,rz = math.rad(PartRotation.X),math.rad(PartRotation.Y),math.rad(PartRotation.Z)|

Camera.CFrame = CFrame.new(CurrentCamera.Position) * CFrame.Angles(rx,ry,rz) * CFrame.Angles( math.rad( math.clamp(-PartRotation.X,-30,30) ),0,0 )

Now the problem is that this only works on some degrees of the Y(yaw) axis

There is this curve where it just breaks completely and I dont know why, maybe someone with a lot of CFrame knowledge knows?

The code snippet you provided doesn’t have a lot, could you share a bit more information about what behavior you’re trying to create?
From a first glance though, it looks like you’re incorrectly clamping and applying the rotation to the camera.
Give something like this a try:

local maxPitch = 30
local minPitch = -30
Camera.CFrame = CFrame.fromEulerAnglesYXZ(math.clamp(rx, minPitch, maxPitch), ry, rz) + CurrentCamera.Position

I’m not sure how your “CurrentCamera” Part is changing its orientation, but maybe this helps.

1 Like

Hmm I did some changes and ended up fixing it up

Camera.CFrame = CFrame.fromEulerAnglesYXZ(rx - math.clamp(rx, minPitch, maxPitch), ry, rz) + CurrentCamera.Position

As far as I know quaternions avoid gimbal lock, but when adding them up separately(that I did for testing purposes) the benefits are removed.
Thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.