I am trying to make a freecam script, and when I rotate the camera 90 degrees to my right or left I can’t look up or down, and when I look behind me the directions are reversed. Someone told me it might be because of gimbal lock so I tried to find a way to switch from Euler angles to a rotation matrix but I couldn’t find a way to do that when I searched it up. I decided to make a new topic since the one before was getting unrelated to the original post. Heres my rotation code:

local delta = UIS:GetMouseDelta()
if rot then
local x,y,z,r0,r1,r2,r10,r11,r12,r20,r21,r22 = cam.CFrame:GetComponents()
local cf = cam.CFrame
cf = CFrame.Angles(-math.rad(delta.Y),-math.rad(delta.X),0) * (cf - cf.Position) + cf.Position
cf = CFrame.lookAt(cf.Position, cf.Position + cf.LookVector)
if delta ~= Vector2.new(0,0) then
cam.CFrame = cam.CFrame:Lerp(cf,.3)
lastCF = cf
end
UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
else
UIS.MouseBehavior = Enum.MouseBehavior.Default
end