Log in to vote

How can I clamp/limit the camera's Y angle to 40 degress being the maximum smoothly?

Asked by 12 days ago

So I've been trying to limit my camera's Y angle to not allow the player to look down more than 40 degrees. I found a way to do it and I did it using math.clamp(), but it ended up not being as smooth as I hoped it would be.

local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer 
local char = script.Parent.Parent 
local humanoid = char:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera

humanoid.CameraOffset =, 4, 0)

runService:BindToRenderStep("LockCursorLoop", Enum.RenderPriority.Camera.Value, function()
    uis.MouseBehavior = Enum.MouseBehavior.LockCenter -- Locks the cursor in the center kind of like shift lock but better I suppose.

runService:BindToRenderStep("CameraAngleLoop", Enum.RenderPriority.Camera.Value, function()
    local rX, rY, rZ = camera.CFrame:ToOrientation()

    local limitX = math.clamp(math.deg(rX), -180, 40)

    camera.CFrame = * CFrame.fromOrientation(math.rad(limitX), rY, rZ)

This is the code I've been using so far. For some reason it's not smooth at all and if I pull the mouse hard enough I can make the player look down beyond 40 degrees.

Here's a demonstration of what I mean by "not smooth":

Thanks in regard.

Answer this question