What is math.clamp?

Well I’m trying to limit the CFrame rotation of a part and also the camera. I have no idea how to do it. But I know math.clamp has something to do with it. But I can’t understand how to use math.clamp actually…

Have you tried checking this post ?

The solution there might help you

Math clamp will set both a minimum and maximum limit to the value you input.
Math clamp take three values, see below:

math.clamp(originalValue, minimumValue, maximumValue)

So for example, if you put math.clamp(5, -2, 10), this means that the maximum value that you put is 10 and the minimum value you put -2, and the original value is 5. This will output 5.

But, if you do, math.clamp(5, 10, 20), this means the max is 20, the min is 10, and your original value is 5. What’s going to happen is that, since the minimum is 10, your original value will become 10.

Few more examples:
math.clamp(10, 200, 400)
Output: 200, since the minimum is 200

math.clamp(200, 100, 150)
Output: 150, since the max is 150

math.clamp(250, 200, 300)
Output: 250, since 250 is within the range of 200-300, so it remains unchanged

7 Likes

So it’s basically this?
math.max(minimumValue,math.min(originalValue,maximumValue))

Dang, i’ve been doing this wrong for several weeks.

Can’t be bothered to process what you put.
But yes, essentially, it keeps the value within your specified range of the max and min.

So how do I limit my camera rotation

Get the cameras CFrame and read the rotational components. Ussualy i would write a script but im a bit unsure about this.

But i can still give you a hint. You should use CFrame:getcomponents. What im unsure is if it returns table lr no

Can u give an example code for a reference

I Know this post is old but heres what I do to limit rotation on my stuff

local pitch = -math.clamp(math.asin(mouse.Hit.Position.Y), math.rad(35), math.rad(35))

the first rad is the minimum rotation
and the second rad is the maximum rotation
and the asin is the value so I think you’d put the carama cframe there.