How to limit player's view in Y axis

Hey! It’s me once again :stuck_out_tongue:

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.

What could I do to fix it?

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)

It’s a bit janky but it does what you asked.

1 Like

Unfortunately the same happens… the player is rotated to 45 degrees instead of only it’s viewing angle being limited

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?

Yeah! Everything is client sided.

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

I see. What about the y axis? Do you want the player to be able to look up while climbing a ladder or restrict them from looking all the way up?

The only problem is with the Y axis, as it rotates the player instead of only limitting it’s view angle.

X axis works perfectly.

Z axis is useless in this case

Im assuming whenever the player climbs the ladder and when they look around, it makes fall off. You don’t want that to happen?

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

Could you possibly show me how this looks right now?

Of course! Here’s a video:

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.

Ah i see now. Can you possibly paste the scripts that is controlling this from the video? Maybe I can look into it and see if I can fix it.

There’s no script controlling it, it’s just roblox’s default climb.

Humanoid. AutoRotate = false is the only thing different from this to any random ladder in any game

I’m not sure how to help then. Sorry if I couldn’t find an solution.

1 Like

That’s fine mate :wink:
Thank you for your interest and effort

Solution:

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)

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