Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Mathf

A collection of common math functions.
Read time 3 minutesLast updated 5 days ago

Definition

  • Type: Struct
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule
public struct Mathf

Static Fields

Value

Description

Deg2RadDegrees-to-radians conversion constant (Read Only).
EpsilonThe smallest positive non-zero value that a float can represent. Using
Mathf.Epsilon
as a delta for equality makes the comparison effectively identical to exact equality on normal-range numbers. For an approximately equal value, use Mathf.Approximately or a tolerance that reflects your data’s scale, typically a combination of absolute and relative tolerances.
InfinityA representation of positive infinity (Read Only).
NegativeInfinityA representation of negative infinity (Read Only).
PIThe well-known 3.14159265358979... value (Read Only).
Rad2DegRadians-to-degrees conversion constant (Read Only).

Static Methods

Method

Description

AbsReturns the absolute value of
value
.
AcosReturns the arc-cosine of f - the angle in radians whose cosine is f.
ApproximatelyCompares two floating point values and returns true if they are similar.
AsinReturns the arc-sine of f - the angle in radians whose sine is f.
AtanReturns the arc-tangent of f - the angle in radians whose tangent is f.
Atan2Returns the angle in radians whose Mathf.Tan is
y/x
.
CeilReturns the smallest integer greater than or equal to f.
CeilToIntReturns the smallest integer greater to or equal to f.
ClampClamps the given value between a range defined by the given minimum integer and maximum integer values. Returns the given value if it is within min and max.
Clamp01Clamps value between 0 and 1 and returns value.
ClosestPowerOfTwoReturns the closest power of two value.
CorrelatedColorTemperatureToRGBConvert a color temperature in Kelvin to RGB color.
CosReturns the cosine of angle f.
DeltaAngleCalculates the shortest difference between two angles.
ExpReturns e raised to the specified power.
FloatToHalfEncode a floating point value into a 16-bit representation.
FloorReturns the largest integer smaller than or equal to f.
FloorToIntReturns the largest integer smaller to or equal to f.
GammaToLinearSpaceConverts the given value from gamma (sRGB) to linear color space.
HalfToFloatConvert a half precision float to a 32-bit floating point value.
InverseLerpDetermines where a value lies between two points.
IsPowerOfTwoReturns true if the value is power of two.
LerpLinearly interpolates between a and b by t.
LerpAngleSame as Mathf.Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees.
LerpUnclampedLinearly interpolates between a and b by t with no limit to t.
LinearToGammaSpaceConverts the given value from linear to gamma (sRGB) color space.
LogReturns the natural (base e) logarithm of a specified number.
Log10Returns the base 10 logarithm of a specified number.
MaxReturns the largest value. When comparing negative values, values closer to zero are considered larger.
MinReturns the smallest of two or more values.
MoveTowardsMoves a value current towards target.
MoveTowardsAngleSame as Mathf.MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.
NextPowerOfTwoReturns the next power of two that is equal to, or greater than, the argument.
PerlinNoiseGenerate 2D Perlin noise.
PerlinNoise1DGenerates a 1D pseudo-random pattern of float values across a 2D plane.
PingPongPingPong returns a value that increments and decrements between zero and the length. It follows the triangle wave formula where the bottom is set to zero and the peak is set to length.
PowReturns f raised to power p.
RepeatLoops the value t, so that it is never larger than length and never smaller than 0.
RoundReturns f rounded to the nearest integer.
RoundToIntReturns f rounded to the nearest integer.
SignReturns the sign of f.
SinReturns the sine of angle f.
SmoothDampGradually moves the current value towards a target value, over a specified time and at a specified velocity.
SmoothDampAngleGradually changes an angle given in degrees towards a desired goal angle over time.
SmoothStepInterpolates between from and to with smoothing at the limits.
SqrtReturns square root of f.
TanReturns the tangent of angle f in radians.