Math
Summary
Various maths functions. See https://docs.unity3d.com/ScriptReference/Mathf.html for further documentation
Class Properties
Name | Return Type | Description |
---|---|---|
deg2Rad | number Read-only | A constant that you multiply with a value in degrees to convert it to radians |
epsilon | number Read-only | The smallest value that a float can have such that 1.0 plus this does not equal 1.0 |
positiveInfinity | number Read-only | Positive Infinity |
negativeInfinity | number Read-only | Negative Infinity |
pi | number Read-only | The value of Pi |
rad2Deg | number Read-only | A constant that you multiply with a value in radians to convert it to degrees |
Class Methods
Math:Abs(f)
The absolute value function
Returns: number (The absolute value of f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Acos(f)
The arc-cosine function
Returns: number (The angle in radians whose cosine is f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Approximately(a, b)
Compares two floating point values if they are similar
Returns: boolean (True if the difference between the values is less than Math.epsilon)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
a | number | The first value | |
b | number | The second value |
Example
Math:Asin(f)
The arc-sine function
Returns: number (The angle in radians whose sine is f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Atan(f)
The arc-tangent function
Returns: number (The angle in radians whose tangent is f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Atan2(y, x)
The two argument arc-tangent function
Returns: number (The angle in radians whose tan is y/x)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
y | number | The numerator value | |
x | number | The denominator value |
Example
Math:Ceil(f)
The ceiling function
Returns: number (The smallest integer greater to or equal to f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Clamp(f, min, max)
Clamps the given value between the given minimum float and maximum float values. Returns the given value if it is within the min and max range
Returns: number (min if f < min, max if f > max otherwise f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value | |
min | number | The minimum value | |
max | number | The maximum value |
Example
Math:Clamp01(value)
Clamps value between 0 and 1 and returns value
Returns: number (0 if f < 0, 1 if f > 1 otherwise f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value | number | The input value |
Example
Math:ClosestPowerOfTwo(value)
Calculates the closest power of two
Returns: number (The closest power of two)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value | number | The input value |
Example
Math:Cos(f)
The cosine function
Returns: number (The cosine of angle f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value in radians |
Example
Math:DeltaAngle(a, b)
Calculates the shortest difference between two given angles
Returns: number (The smaller of the two angles in degrees between input and target)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
a | number | The first value in degrees | |
b | number | The second value in degrees |
Example
Math:Exp(power)
The exponent function
Returns: number (Returns e raised to the specified power)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
power | number | The input value |
Example
Math:Floor(f)
The floor function
Returns: number (The largest integer that is less than or equal to the input)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:InverseLerp(min, max, t)
Inverse linear interpolation between two values by given ratio
Returns: number (A value between 0 and 1 representing how far t is between min and max)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
min | number | The minimum value | |
max | number | The maximum value | |
t | number | The input value |
Example
Math:IsPowerOfTwo(value)
Determines whether a value is a power of two
Returns: boolean (The logarithm of f in base b)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value | number | The input value |
Example
Math:Lerp(min, max, t)
Linearly interpolates two floats by a ratio
Returns: number (A value between min and max representing how far t is between 0 and 1)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
min | number | The minimum value | |
max | number | The maximum value | |
t | number | The input value |
Example
Math:LerpAngle(min, max, a)
Same as Lerp but takes the shortest path between the specified angles wrapping around a circle
Returns: number (An angle between min and max representing how far t is between 0 and 1)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
min | number | The start angle in degrees | |
max | number | The end angle in degrees | |
a | number | The input value in degrees |
Example
Math:LerpUnclamped(min, max, t)
Same as Math:Lerp but allows extrapolated values
Returns: number (A value representing t scaled from the range 0:1 to a new range min:max)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
min | number | The minimum value | |
max | number | The maximum value | |
t | number | The input value |
Example
Math:Log(f, b)
The logarithm of a specified number in a specified base
Returns: number (The logarithm of f in base b)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value | |
b | number | The base |
Example
Math:Log10(f)
The base 10 logarithm of a specified number
Returns: number (The base 10 logarithm of a specified number)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Max(a, b)
The larger of two float numbers
Returns: number (The largest of a and b)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
a | number | The first input value | |
b | number | The second input value |
Example
Math:Max(values)
The largest value in a sequence of float numbers
Returns: number (The largest value in the list)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
values | number[] | A list of numbers |
Example
Math:Min(a, b)
The smaller of two float numbers
Returns: number (The smaller of a and b)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
a | number | The first input value | |
b | number | The second input value |
Example
Math:Min(values)
The smallest value in a sequence of float numbers
Returns: number (The smallest value in a sequence of float numbers)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
values | number[] | A list of numbers |
Example
Math:MoveTowards(current, target, maxDelta)
Moves a value towards a target value by a given amount
Returns: number (The input + or - maxDelta but clamped to it won't overshoot the target value)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
current | number | The input value | |
target | number | The target value | |
maxDelta | number | The largest change allowed each time |
Example
Math:NextPowerOfTwo(value)
The smallest power of two greater than or equal to the specified number
Returns: number (The smallest power of two greater than or equal to the specified number)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value | number | The input value |
Example
Math:PerlinNoise(x, y)
Samples a two-dimensional Perlin noise map
Returns: number (Returns the value of the perlin noise as coordinates x,y)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
x | number | The input value | |
y | number | The power to raise to |
Example
Math:PingPong(t, length)
Similar to Math:Round except the values alternate between forward and backwards in the range
Returns: number (A value that is never larger than length and never smaller than 0)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
t | number | The input value | |
length | number | The upper limit |
Example
Math:Pow(f, p)
The raised to the specified power
Returns: number (Returns f raised to the specified power)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value | |
p | number | The power to raise to |
Example
Math:Repeater(t, length)
Loops the value t - similar to "clock" arithmetic
Returns: number (A value that is never larger than length and never smaller than 0)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
t | number | The input value | |
length | number | The upper limit |
Example
Math:Round(f)
The rounding function
Returns: number (The nearest integer value to f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Sign(f)
The sign function
Returns: number (The sign of f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Sin(f)
The sine function
Returns: number (The sine of angle f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value in radians |
Example
Math:Sqrt(f)
The square root function
Returns: number (The square root of f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:SmoothStep(from, to, t)
The smoothstep function
Returns: number (The input smoothly interpolated between the range [from, to] by the ratio t)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
from | number | The lower range | |
to | number | The upper range | |
t | number | The input value |
Example
Math:Tan(f)
The tangent of an angle
Returns: number (The tangent of an angle)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value |
Example
Math:Sinh(f)
The hyperbolic sine function
Returns: number (The hyperbolic sine of f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value in radians |
Example
Math:Cosh(f)
The hyperbolic cosine function
Returns: number (The hyperbolic cosine of f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value in radians |
Example
Math:Tanh(f)
The hyperbolic tangent function
Returns: number (The hyperbolic tangent of f)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
f | number | The input value in radians |
Example
Last updated