Limit camera rotation

Hello, I am trying to limit the camera rotation to 30 degrees to right and 30 degrees to the left. So the player can only rotate their camera 30 degrees to each direction. But I can’t do it. I already tried some scripts, but none of them worked. I even searched in the forum, but I couldn’t find anything related. The most I got was this script that limits the rotation to 30 degrees in only one direction.

local minRotation = -30
local maxRotation = 30
local lastRotation = 0

game:GetService("RunService").RenderStepped:Connect(function()
	local currentRotation = workspace.CurrentCamera.CFrame.LookVector
	local angle = currentRotation:Dot(Vector3.new(1,0,0))
	angle = math.deg(math.acos(angle))
		if angle > lastRotation then
		    if angle > maxRotation then
			   angle = maxRotation
		    end
		else
		if angle < minRotation then
			angle = minRotation 
		end	
	end
	lastRotation = angle
	local newRotaton = Vector3.new(math.cos(math.rad(angle)),0,math.sin(math.rad(angle)))
	workspace.CurrentCamera.CFrame = CFrame.new(workspace.CurrentCamera.CFrame.p,workspace.CurrentCamera.CFrame.p + newRotaton)
end)

Current result:
https://gyazo.com/d55fb430175af08b83a143ef0a7ae65a

1 Like

I found an old script that may help you out x

RunService.Heartbeat:Connect(function()
		local Cam = Camera.CFrame:ToOrientation()	
		local Val = math.clamp(math.deg(Cam), -30, 30)
		Camera.CFrame = CFrame.new(Camera.CFrame.p) * CFrame.fromOrientation(math.rad(Val), math.rad(170), 0)	
	end)

It limits the camera’s movement to a limited angle on the X axis and prevents it from moving on the Y axis. But alas, it’s not smooth at all, and I haven’t found anything to make it completely smooth anywhere in the forum. Hopefully this can help you! xo

1 Like

This code only works in X direction, when I try to change to the Z direction it does nothing.

Z direction is roll angle, and player cameras do not move in the Z direction by default.
You should try changing that script to the Y direction.