Problem with camera rotation

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
1 Like
local X, Y = Delta.X, Delta.Y

cameraAngleY = math.clamp(cameraAngleY - Y * 0.4, -75, -5)
cameraAngleX -= X

local currentCFrame = CFrame.Angles(
0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0
)

Not sure if it works. This is my script form before, the clamp might be incorrect, I think 0.4 is the sensitivity.

1 Like

I already have this part because this is basically already how I am rotating the camera except you have clamps on it. Thanks though

This is the problem, you need to separate delta X and Y.

Also you have to add a limit for Y axis because that would invert the rotation.

2 Likes

Im not sure why but the same problem keeps happening even when I separate them.

1 Like

I found the solution I just had to rotate the camera on the local axis instead of the world x axis

heres the working rotate code:

	local delta = UIS:GetMouseDelta()
	if rot then
		local cf = cam.CFrame
		cf *= CFrame.Angles(-math.rad(delta.Y),0,0)
		cf = CFrame.Angles(0,-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,sens)
			lastCF = cf
		end
		UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
	else
		UIS.MouseBehavior = Enum.MouseBehavior.Default
	end
1 Like