Help with math.clamp

I am trying to create a drive by system but the hand moves into the body. I tried using math.clamp but it does not work properly. How can I fix this?

WITHOUT MATH.CLAMP

WITH MATH.CLAMP

This is the code I created

runservice.RenderStepped:Connect(function(deltaTime)
	local rx, ry, rz = CAMERA.CFrame:ToOrientation() 
	local fx, fy, fz = character.HumanoidRootPart.CFrame:ToOrientation()
	rightShoulder.C0 = CFrame.new(rightShoulder.C0.p) * CFrame.Angles((rx)+ math.rad(115),0,0) * CFrame.Angles(0,0,math.clamp( -ry+ fy,-.8,50) )
end)

Start with putting -.8 and 50 into math.rad.

When using frames they always use rad so try converting your degrees into radians like how @Darkmist101 said previously.

1 Like

Did not work for me had a similar effect to the second video

Sorry for this late reply but have you tried looking at other topics and resources to help you with problem you are having? - I have looked into other topics and it seems as if you will need to turn this into a whole cframe and then clamp like so:

local ConstructCFrame = CFrame.new(rightShoulder.C0.p) * CFrame.Angles((rx)+ math.rad(115),0,0) * CFrame.Angles(0,0,-ry+ fy)
local OrientationX, OrientationY,OrientationZ = ConstructCFrame:ToOrientation()
local MinLimit, MaxLimit = -.8, 50
local ClampedAxis = math.clamp( OrientationZ , math.rad(MinLimit) , math.rad(MaxLimit) )
local NewCFrame = CFrame.new( ConstructCFrame.Position ) * CFrame.fromOrientation( OrientationX , OrientationY, ClampedAxis) 
1 Like

Thanks for helping me. I have figured it out. I used this below then used math.clamp and it worked

local rx,ry,rz =  character.HumanoidRootPart.CFrame:ToObjectSpace(CAMERA.CFrame):ToOrientation()

Oh how great I forgot about object space when I have a script that uses it for character rotation as well. I still made some mistake on my showcase so if you want to use I or anyone I will fix it to accommodate for the errors.