Locking Camera View Height

Hey,
How would I go about locking the camera view so you can’t look down at the floor or up at the roof and can only look forward (they’re in first person) and up-down a few degrees?

Discord: lukemango#5552

Thanks,
Luke

2 Likes
local maxrot = math.rad(30)

game:GetService("RunService").RenderStepped:Connect(function()
local ccf = cam.CFrame
local xr,yr,zr = ccf:ToEulerAnglesXYZ()
local nxr = math.clamp(xr,-maxrot,maxrot)
if xr~=nxr then
cam.CFrame = CFrame.new(ccf.Position)*CFrame.Angles(nxr,yr,zr)
end
end)

On mobile so no tabbing.

4 Likes

Thankyou, i’ll test this out!

<3

Tried that out, but it only forces them to look left,
how can I change it so they look 90 degrees right instead?

Just add 90 degrees to yr used in the CFrame.Angles …

1 Like

WDYM 90 degrees left or right? The code I gave you limits the camera on the x, not y-axis.

https://gyazo.com/499783dcb712be18e444f556d45cad3f

A bit of a necrobump, but OK.

local maxrot = math.rad(30)

game:GetService("RunService").Heartbeat:Connect(function()
	local cam = workspace.CurrentCamera
	local ccf = cam.CFrame
	local xr,yr,zr = ccf:ToOrientation()
	local nxr = math.clamp(xr,-maxrot,maxrot)
	if xr~=nxr then
		cam.CFrame = CFrame.new(ccf.Position)*CFrame.fromOrientation(nxr,yr,zr)
	end
end)
1 Like

It’s a good script, although when I reach the max height the camera is a bit shaky.