Math

Summary

Various maths functions. See https://docs.unity3d.com/ScriptReference/Mathf.html for further documentation

Class Properties

NameReturn TypeDescription

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:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Abs(0.1)

Math:Acos(f)

The arc-cosine function

Returns: number (The angle in radians whose cosine is f)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Acos(0.1)

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:

NameTypeDefaultDescription

a

number

The first value

b

number

The second value

Example

nearlySame = Math:Approximately(0.1000000000000000011, 0.100000000000000001)

Math:Asin(f)

The arc-sine function

Returns: number (The angle in radians whose sine is f)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Asin(0.1)

Math:Atan(f)

The arc-tangent function

Returns: number (The angle in radians whose tangent is f)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Atan(0.1)

Math:Atan2(y, x)

The two argument arc-tangent function

Returns: number (The angle in radians whose tan is y/x)

Parameters:

NameTypeDefaultDescription

y

number

The numerator value

x

number

The denominator value

Example

result = Math:Atan2(0.1, 3)

Math:Ceil(f)

The ceiling function

Returns: number (The smallest integer greater to or equal to f)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Ceil(0.1)

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:

NameTypeDefaultDescription

f

number

The input value

min

number

The minimum value

max

number

The maximum value

Example

result = Math:Clamp(input, -1, 1)

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:

NameTypeDefaultDescription

value

number

The input value

Example

result = Math:Clamp01(1.3)

Math:ClosestPowerOfTwo(value)

Calculates the closest power of two

Returns: number (The closest power of two)

Parameters:

NameTypeDefaultDescription

value

number

The input value

Example

result = Math:ClosestPowerOfTwo(13)

Math:Cos(f)

The cosine function

Returns: number (The cosine of angle f)

Parameters:

NameTypeDefaultDescription

f

number

The input value in radians

Example

result = Math:Cos(0.1)

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:

NameTypeDefaultDescription

a

number

The first value in degrees

b

number

The second value in degrees

Example

result = Math:DeltaAngle(1080, 90)

Math:Exp(power)

The exponent function

Returns: number (Returns e raised to the specified power)

Parameters:

NameTypeDefaultDescription

power

number

The input value

Example

result = Math:Exp(100)

Math:Floor(f)

The floor function

Returns: number (The largest integer that is less than or equal to the input)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Floor(2.1)

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:

NameTypeDefaultDescription

min

number

The minimum value

max

number

The maximum value

t

number

The input value

Example

result = Math:InverseLerp(min, max, 23)

Math:IsPowerOfTwo(value)

Determines whether a value is a power of two

Returns: boolean (The logarithm of f in base b)

Parameters:

NameTypeDefaultDescription

value

number

The input value

Example

isPower = Math:IsPowerOfTwo(value)

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:

NameTypeDefaultDescription

min

number

The minimum value

max

number

The maximum value

t

number

The input value

Example

result = Math:Lerp(-1, 1, 0.25)

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:

NameTypeDefaultDescription

min

number

The start angle in degrees

max

number

The end angle in degrees

a

number

The input value in degrees

Example

result = Math:LerpAngle(-30, 90, angle)

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:

NameTypeDefaultDescription

min

number

The minimum value

max

number

The maximum value

t

number

The input value

Example

result = Math:Lerp(-1, 1, 0.25)

Math:Log(f, b)

The logarithm of a specified number in a specified base

Returns: number (The logarithm of f in base b)

Parameters:

NameTypeDefaultDescription

f

number

The input value

b

number

The base

Example

result = Math:Log(input, 2)

Math:Log10(f)

The base 10 logarithm of a specified number

Returns: number (The base 10 logarithm of a specified number)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Log10(0.1)

Math:Max(a, b)

The larger of two float numbers

Returns: number (The largest of a and b)

Parameters:

NameTypeDefaultDescription

a

number

The first input value

b

number

The second input value

Example

biggest = Math:Max(a, b)

Math:Max(values)

The largest value in a sequence of float numbers

Returns: number (The largest value in the list)

Parameters:

NameTypeDefaultDescription

values

number[]

A list of numbers

Example

biggest = Math:Max({1, 4, 6, 2, -3, 32, 5})

Math:Min(a, b)

The smaller of two float numbers

Returns: number (The smaller of a and b)

Parameters:

NameTypeDefaultDescription

a

number

The first input value

b

number

The second input value

Example

smallest = Min:Min(a, b)

Math:Min(values)

The smallest value in a sequence of float numbers

Returns: number (The smallest value in a sequence of float numbers)

Parameters:

NameTypeDefaultDescription

values

number[]

A list of numbers

Example

smallest = Math:Min({1, 4, 6, 2, -3, 32, 5})

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:

NameTypeDefaultDescription

current

number

The input value

target

number

The target value

maxDelta

number

The largest change allowed each time

Example

x = Math:MoveTowards(x, 10, 0.5)

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:

NameTypeDefaultDescription

value

number

The input value

Example

result = Math:NextPowerOfTwo(26)

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:

NameTypeDefaultDescription

x

number

The input value

y

number

The power to raise to

Example

result = Math:PerlinNoise(0.4, 1.2)

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:

NameTypeDefaultDescription

t

number

The input value

length

number

The upper limit

Example

result = Math:PingPong(0.4, 1.2)

Math:Pow(f, p)

The raised to the specified power

Returns: number (Returns f raised to the specified power)

Parameters:

NameTypeDefaultDescription

f

number

The input value

p

number

The power to raise to

Example

result = Math:Pow(0.1, 16)

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:

NameTypeDefaultDescription

t

number

The input value

length

number

The upper limit

Example

result = Math:Round(0.1)

Math:Round(f)

The rounding function

Returns: number (The nearest integer value to f)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Round(0.1)

Math:Sign(f)

The sign function

Returns: number (The sign of f)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Sign(0.1)

Math:Sin(f)

The sine function

Returns: number (The sine of angle f)

Parameters:

NameTypeDefaultDescription

f

number

The input value in radians

Example

result = Math:Sin(0.1)

Math:Sqrt(f)

The square root function

Returns: number (The square root of f)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Sqrt(0.1)

Math:SmoothStep(from, to, t)

The smoothstep function

Returns: number (The input smoothly interpolated between the range [from, to] by the ratio t)

Parameters:

NameTypeDefaultDescription

from

number

The lower range

to

number

The upper range

t

number

The input value

Example

result = Math:SmoothStep(0, 1, 0.5)

Math:Tan(f)

The tangent of an angle

Returns: number (The tangent of an angle)

Parameters:

NameTypeDefaultDescription

f

number

The input value

Example

result = Math:Tan(0.1)

Math:Sinh(f)

The hyperbolic sine function

Returns: number (The hyperbolic sine of f)

Parameters:

NameTypeDefaultDescription

f

number

The input value in radians

Example

result = Math:Sinh(0.1)

Math:Cosh(f)

The hyperbolic cosine function

Returns: number (The hyperbolic cosine of f)

Parameters:

NameTypeDefaultDescription

f

number

The input value in radians

Example

result = Math:Cosh(0.1)

Math:Tanh(f)

The hyperbolic tangent function

Returns: number (The hyperbolic tangent of f)

Parameters:

NameTypeDefaultDescription

f

number

The input value in radians

Example

result = Math:Tanh(0.1)

Last updated