Hello, I currently have a script that limits a player’s rotation of the first-person camera on the Y-axis. It does this well, however it additionally limits rotation on the X-axis which I do not want it to do. I have analyzed the script and don’t know how I can write it so that only the Y-axis gets limited.
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
RunService.RenderStepped:Connect(function()
local Cam = Camera.CFrame:ToOrientation()
local Val = math.clamp(math.deg(Cam), -50, 100)
Camera.CFrame = CFrame.new(Camera.CFrame.p) * CFrame.fromOrientation(math.rad(Val), math.rad(170), 0)
end)
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
RunService.RenderStepped:Connect(function()
local Cam = table.pack(Camera.CFrame:ToOrientation()) -- Order is Z,X,Y
local Val = math.clamp(math.deg(Cam[3]), -50, 100)
Camera.CFrame = CFrame.new(Camera.CFrame.p) * CFrame.fromOrientation(Cam[2], math.deg(Val), Cam[1])
end)
Yes, It is. This is because my game has a script where you can see your own body. If you look down all the way you can see the top of your torso which ruins the effect (see image). I want to stop the camera before then.
Okay, so allow me to elaborate on a few things for clarity.
The code you originally sent does a few things. First of all, ToOrientation() returns a tuple in the order of (Z-axis, X-axis, Y-axis). When you make the Val variable, math.deg(Cam) is equal to the degrees of the Z-axis due to it being the first number in the tuple. Val is now equal to a limitation of the Z-axis between -50 and 100.
That isn’t the only issue. It then changes the CFrame orientation of the camera by first getting the original CFrame, then creating a fromOrientation CFrame. Which is correct. The first parameter of the fromOrientation function is the X-axis. In your code, the X-axis set to the radian of the limited Z-axis (variable Val). The Y-axis is set to a constant 170 degrees, and does not change no matter what, due to it being a constant number. The Z-axis is set to 0 radians, which is also constant (obviously).
Therefore, the original code only changes the X-axis of the camera’s CFrame. The Y and Z axes were not touched. It was never limiting the Y-axis.