Math.clamp explanation?

Hi, I recently came over math.clamp, but I did not really understand it.
Could someone kindly explain what it does, how to use it and what it can do? Thanks, (yes i did read the math.clamp devforum post by the gamer101)

37 Likes

It takes 3 values. X, min and max. If X is in the range between min and max it returns X. If X is lower than min it returns min and if X is higher than max it returns max. This can be useful for tons of applications, but a good example would be constraining rotation on an axis. If you want a player to control the rotation of a brick on the Y axis but only in a 90 degree window you could clamp it’s total rotation.

162 Likes

math.clamp(x, y, z)

x is the value you want to clamp

y is the minimum value x can be

z is the maximum value x can be

math.clamp(5,1,6) returns 5
math.clamp(0,1,6) returns 1
math.clamp(7,1,6) returns 6

math.clamp() is useful for keeping a number (x) in between y and z inclusive.

Edit: Changed duplicated y to z for clarity

105 Likes