I’m now trying to limit the player’s camera in first person, but I’ve faced a problem that has been avoiding me to do that in a while. I’m basically using this code to achieve this:
local RS = game:GetService("RunService")
local Camera = workspace.CurrentCamera
RS:BindToRenderStep("UpdateLoop", Enum.RenderPriority.Camera.Value, function()
local X, Y, Z = Camera.CFrame:ToOrientation()
local limitX = math.clamp(math.deg(X), -45, 45)
local limitY = math.clamp(math.deg(Y), -45, 45)
Camera.CFrame = CFrame.new(Camera.CFrame.p) * CFrame.fromOrientation(math.rad(limitX), math.rad(limitY), Z)
end)
It works like a charm with the X axis, but with the Y axis it forces the player view to 45 degrees, basically rotating him to this angle, instead of limiting it to not fully move left/right.
Have you tried CFrame.angles? Instead of setting the Y-axis rotation with CFrame.fromOrientation, you can use CFrame.Angles to rotate only around the Y-axis while leaving the other axes unchanged.
Try this:
local RS = game:GetService("RunService")
local Camera = workspace.CurrentCamera
RS:BindToRenderStep("UpdateLoop", Enum.RenderPriority.Camera.Value, function()
local CF = Camera.CFrame
local X, Y, Z = CF:ToOrientation()
local limitX = math.clamp(math.deg(X), -45, 45)
local limitY = math.clamp(math.deg(Y), -45, 45)
Camera.CFrame = CFrame.new(CF.p) * CFrame.Angles(0, math.rad(limitY), 0) * CFrame.fromOrientation(math.rad(limitX), 0, 0)
end)
What exactly are you trying to achieve? Are you wanting the player to not be able to look around in 360 degrees? Also, is this placed in a local script?
And yes I don’t want to let the player look around 360, because I have set Humanoid.AutoRotate to false and I want to avoid issues of the player looking back while climbing a ladder
Nope, Humanoid. AutoRotate = false already fix it up. The problem is that the player can rotate it’s camera 360, making him look like the girl from the exorcist
I want to limit it, so the player cannot fully rotates it’s camera on the Y axis. I want to achieve this dynamically. My current code and your proposed one rotates the player to 45 degrees and then locks it’s view, which is not good, considering that him can climb a ladder at any orientation.
local p = game.Players.LocalPlayer
local Char = p.Character or p.CharacterAdded:Wait()
local HRP = Char:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
local RS = game:GetService("RunService")
local camLimit = RS.Stepped:Connect(function()
local CameraCFrame = HRP.CFrame:ToObjectSpace(camera.CFrame)
local x, y, z = CameraCFrame:ToOrientation()
local a = camera.CFrame.Position.X
local b = camera.CFrame.Position.Y
local c = camera.CFrame.Position.Z
local ylimit = math.rad(math.clamp(math.deg(y),-30,30))
camera.CFrame = HRP.CFrame:ToWorldSpace(CFrame.new(a,b,c) * CFrame.fromOrientation(x,ylimit,z))
end)