clamp

Returns a number within a range. You can use the clamp function to confine an increasing, decreasing, or randomly changing number to a range of values.

float clamp(float minnumber, float maxnumber, float parameter)

minnumber and maxnumber specify the range of the returned value.

parameter is an attribute or variable whose value you want to clamp within the range.

If parameter is within the numerical range of minnumber and maxnumber, the function returns the value of parameter.

If parameter is greater than the range, the function returns the maxnumber.

If parameter is less than the range, the function returns the minnumber.

Examples

clamp(4,6,22)

Returns 6, because 22 is greater than 6, the maximum number of the range.

clamp(4,6,2)

Returns 4, because 2 is less than 4, the minimum number of the range.

clamp(4,6,5)

Returns 5, because it’s within the range.

Ball.scaleY = clamp(0,3,time);

Returns a value between 0 and 3 each time the expression executes.

When you rewind the animation to frame 1, the above expression executes and Ball’s scaleY attribute receives the value of time—a number slightly above 0. The clamp function returns the value of time because time is within the range 0 to 3.

When you play the animation, time increments slightly with each frame. The expression executes with each frame and Ball’s scaleY attribute receives the value of time until time exceeds 3. When time exceeds 3, the clamp function returns the value 3.