Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years 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 = Vector3.new(0, 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.
end)


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.new(camera.CFrame.Position) * CFrame.fromOrientation(math.rad(limitX), rY, rZ)
end)

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": https://gyazo.com/eb36eb350e2b7345075314522b0effa0

Thanks in regard.

Answer this question