Restricting camera movement to the sides problems

  1. I want to at certain times restrict players head/camera movement, like this:
    robloxapp-20230609-1305585.wmv (1.7 MB)
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera

RunService:BindToRenderStep("UpdateLoop", Enum.RenderPriority.Camera.Value, function()
		local rX, rY, rZ = camera.CFrame:ToOrientation()
		local limX = math.clamp(math.deg(rX), -45, 45)--Restricts movement up and down
		local limY = math.clamp(math.deg(rY), -45, 45) --Restricts movement to the sides
		print(math.deg(rY))
		camera.CFrame = CFrame.new(camera.CFrame.p) * CFrame.fromOrientation(math.rad(limX), math.rad(limY), rZ)	
end)
  1. Every thing works fine for angles -45 and 45 as you can see in the video but if I were to use for example values from 135 to 225 it wouldn’t work for example:
local limY = math.clamp(math.deg(rY),135, 225) 

robloxapp-20230609-1312212.wmv (1.3 MB)

Thanks in advance!!

please don’t send .wmv files

Rotation ranges from [-180] to [180] degrees. A slight adjustment to the limY clamp should do.

image

Going counter-clock wise, this is actually the area between 135 deg and -135 deg (180 - (250-180)), if I’m not mistaken. The clamp has to be broken into two parts: -135, -180 and 135, 180.

local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera

local clamp = math.clamp
local deg = math.deg
local rad = math.rad

RunService:BindToRenderStep("UpdateLoop", Enum.RenderPriority.Camera.Value, function()
	local rX, rY, rZ = camera.CFrame:ToOrientation()
	local limX = clamp(deg(rX), -30, 30)
	local limY = rY < 0 and clamp(deg(rY), -180, -135)  or clamp(deg(rY), 135, 180)
	print(limY)
	camera.CFrame = CFrame.new(camera.CFrame.p) * CFrame.fromOrientation(rad(limX), rad(limY), rZ)	
end)

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