Mathf
TypeStruct (only static members)
NamespaceUnityEngine
Works withfloat

Mathf

Mathf is a toolbox of math functions for float numbers: clamping, rounding, smooth movement, trigonometry and more. It's Unity's version of C#'s System.Math, but works with float instead of double.

Constants

NameDescription
Mathf.PI3.14159...
Mathf.Deg2Rad / Mathf.Rad2DegMultiply to convert degrees to radians, or back.
Mathf.InfinityA number bigger than any other. Handy as a starting "closest distance".

Most-used methods

MethodDescriptionExample
Clamp(value, min, max)Keep a value inside a range.Clamp(150, 0, 100) = 100
Clamp01(value)Clamp between 0 and 1.Clamp01(1.4f) = 1
Lerp(a, b, t)Value t of the way from a to b (t is clamped 0-1).Lerp(0, 10, 0.25f) = 2.5
InverseLerp(a, b, value)The opposite: how far along is value? Returns 0-1.InverseLerp(0, 200, 50) = 0.25
MoveTowards(current, target, maxDelta)Step toward a target, never overshooting.MoveTowards(0, 10, 3) = 3
SmoothDamp(current, target, ref velocity, smoothTime)Spring-like smooth approach.Camera zoom
Abs(x)Remove the minus sign.Abs(-4) = 4
Sign(x)1 if positive or zero, -1 if negative.Sign(-3) = -1
Min(a, b) / Max(a, b)Smaller / larger value.Max(2, 7) = 7
RoundToInt(x)Round to nearest int. Halves go to the nearest even number.RoundToInt(2.5f) = 2
FloorToInt(x) / CeilToInt(x)Round down / up to an int.FloorToInt(2.9f) = 2
Sqrt(x), Pow(x, p)Square root, power.Pow(2, 3) = 8
Sin(rad), Cos(rad)Trig. Takes radians.Sin(Time.time) bobs -1 to 1
Atan2(y, x)Angle (radians) of a direction. Great for 2D aiming.See below
PingPong(t, length)Goes 0 → length → 0 → ... as t increases.Back-and-forth platforms
Repeat(t, length)Like % (modulo) but works with floats and negatives. Wraps around.Repeat(370, 360) = 10
DeltaAngle(a, b)Shortest difference between two angles in degrees.DeltaAngle(350, 10) = 20
Approximately(a, b)Are two floats nearly equal?See Common Mistakes

Code Samples

Health that can't go below 0 or above max

currentHealth = Mathf.Clamp(currentHealth - damage, 0, maxHealth);

A floating, bobbing pickup

using UnityEngine;

public class Bob : MonoBehaviour
{
    [SerializeField] private float height = 0.25f;
    [SerializeField] private float speed = 2f;
    private Vector3 startPos;

    void Start() { startPos = transform.position; }

    void Update()
    {
        // Sin goes smoothly between -1 and 1 as time passes.
        float offset = Mathf.Sin(Time.time * speed) * height;
        transform.position = startPos + Vector3.up * offset;
    }
}

Health bar fill amount

// Turn "37 out of 120" into a 0-1 value for a UI Image's fillAmount.
healthBar.fillAmount = Mathf.InverseLerp(0, maxHealth, currentHealth);

2D: rotate a sprite to face the mouse

Vector3 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 dir = mouse - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, angle);

Smooth camera zoom

private float zoomVelocity; // SmoothDamp needs a field to remember speed between frames

void Update()
{
    cam.fieldOfView = Mathf.SmoothDamp(cam.fieldOfView, targetFov, ref zoomVelocity, 0.2f);
}

Common Mistakes

See also