I want to make it so that you cannot move your camera behind the character while climbing walls in first person. This is working fine with most orientations of the HumanoidRootPart. But when the HumanoidRootParts Y orientation is 180 and I subtract 45, it prints -255 (180 - 45 = -255?). Now, this is entirely wrong. It should be 135 and not -225. I tried using math.rad(), math.deg() and I also went so far as to create a function. Nothing has worked so far.
This is the code.
local rX, rY, rZ = Camera.CFrame:ToOrientation()
local ClampMin = HumanoidRootPart.Orientation + Vector3.new(0,-45,0)
local ClampMax = HumanoidRootPart.Orientation + Vector3.new(0,45,0)
print(ClampMin.Y)
print(ClampMax.Y)
local limY = math.clamp(math.deg(rY), ClampMin.Y, ClampMax.Y)
Camera.CFrame = CFrame.new(Camera.CFrame.p) * CFrame.fromOrientation(rX, math.rad(limY), rZ)
Does anyone have any suggestions on how to fix this?
I am very grateful for any help I have been trying to fix this problem for 1 hour.
This is just how Studio tends to handle angles. Depending on what you want to do, you’ll need to either add or subtract 360 to get a desired outcome. Not sure how helpful this will be, but keep me updated.
Just to add more detail to what I want to do. I want the camera to be clamped to -45, 45 relative to the HumanoidRootParts orientation. I’m not trying to position the camera, I want to clamp it. And if the HumanoidRootParts orientation is 180, it for some reason prints -255 instead of 135.
While trying to fix the error I found sort of a solution. It now works but now instead of it breaking everything it shows me this error:
Workspace.SchlossGarage.Movements:171: invalid argument #3 to ‘clamp’ (max must be greater than or equal to min)
And I know what the error is and how to fix it. But I don’t know how to fix it in this case.
I’ve tried using math.min() and math.max() to get the smaller and bigger number and that works except it doesn’t give the player a 45 degrees view but rather a 135 degrees view.
This is the new code:
local rX, rY, rZ = Camera.CFrame:ToOrientation()
local OrientationCFrame = HumanoidRootPart.CFrame * CFrame.Angles(0,math.rad(-45),0)
local CRX, CRY, CRZ = OrientationCFrame:ToOrientation()
local OrientationCFrame2 = HumanoidRootPart.CFrame * CFrame.Angles(0,math.rad(45),0)
local CRX2, CRY2, CRZ2 = OrientationCFrame2:ToOrientation()
local ClampMin = Vector3.new(0,math.deg(CRY),0)
local ClampMax = Vector3.new(0,math.deg(CRY2),0)
print(ClampMin.Y)
print(ClampMax.Y)
local limY = math.clamp(math.deg(rY), ClampMin.Y, ClampMax.Y)
Camera.CFrame = CFrame.new(Camera.CFrame.p) * CFrame.fromOrientation(rX, math.rad(limY), rZ)
Is it even possible to clamp it so it works or is that just how clamping works?
If there’s an alternative way to clamp the camera like this, please let me know.