Mathf (Unity Math Library)
This page covers how to use Mathf, Unity's built-in math library. It is similar to C#'s Math class, but Mathf is specialized for float types and provides common game development utilities such as interpolation, clamping, and trigonometric functions.
Syntax
using UnityEngine; // Returns the absolute value (float version). float absValue = Mathf.Abs(float f); // Clamps a value between min and max (inclusive). float clamped = Mathf.Clamp(float value, float min, float max); // Linearly interpolates from a to b by t (0 to 1). float interpolated = Mathf.Lerp(float a, float b, float t); // Trigonometric functions (argument in radians). float sine = Mathf.Sin(float f); float cosine = Mathf.Cos(float f); // Converts degrees to radians. float radians = degrees * Mathf.Deg2Rad; // Converts radians to degrees. float degrees = radians * Mathf.Rad2Deg;
Method List
| Method / Constant | Description |
|---|---|
| Mathf.Abs(f) | Returns the absolute value of f. |
| Mathf.Clamp(value, min, max) | Returns value clamped to the range [min, max]. |
| Mathf.Clamp01(value) | Clamps value to the range [0, 1]. Useful for normalizing values. |
| Mathf.Lerp(a, b, t) | Linearly interpolates between a and b by t (0 to 1). Returns a when t is 0, and b when t is 1. |
| Mathf.LerpUnclamped(a, b, t) | Same as Lerp, but t is not clamped to [0, 1]. Values outside that range are allowed. |
| Mathf.Sin(f) / Mathf.Cos(f) | Returns the sine or cosine of f (in radians). |
| Mathf.Sqrt(f) | Returns the square root of f. |
| Mathf.Pow(f, p) | Returns f raised to the power p. |
| Mathf.Floor(f) / Mathf.Ceil(f) | Returns the floor or ceiling of f as a float. |
| Mathf.Round(f) | Rounds f to the nearest integer. |
| Mathf.PI | The constant π (3.14159265...). |
| Mathf.Deg2Rad | Conversion factor from degrees to radians (π / 180). |
| Mathf.Rad2Deg | Conversion factor from radians to degrees (180 / π). |
| Mathf.Infinity | A constant representing positive infinity. |
Sample Code
using UnityEngine;
public class MathfSample : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f;
[SerializeField] private Transform target;
void Start()
{
// Mathf.Clamp() — clamps the health value to the range [0, 100].
float health = 120f;
health = Mathf.Clamp(health, 0f, 100f);
Debug.Log($"Health (clamped): {health}"); // 100
// Mathf.Abs() — gets the absolute value of a distance difference.
float diff = -3.5f;
Debug.Log($"Absolute value: {Mathf.Abs(diff)}"); // 3.5
// Trigonometry — creates a back-and-forth animation using a sine wave.
float swing = Mathf.Sin(Time.time) * 2f;
Debug.Log($"Sin value: {swing}");
// Converts an angle from degrees to radians.
float angle = 45f;
float radians = angle * Mathf.Deg2Rad;
Debug.Log($"Sin(45°) = {Mathf.Sin(radians):F4}"); // 0.7071
}
void Update()
{
// Mathf.Lerp() — smoothly moves toward the target.
if (target != null)
{
// Multiply by Time.deltaTime to make the speed frame-rate independent.
float t = moveSpeed * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, target.position, t);
}
// Mathf.Clamp01() — normalizes the input to the range [0, 1].
float input = Input.GetAxis("Horizontal");
float normalized = Mathf.Clamp01(Mathf.Abs(input));
}
}
Notes
All Mathf functions operate on float values. C#'s Math class uses double, so prefer Mathf in Unity projects.Trigonometric functions take arguments in radians, not degrees. To use degrees, multiply by Mathf.Deg2Rad before passing the value.
When using Mathf.Lerp() inside Update(), multiplying t by Time.deltaTime produces smooth, frame-rate-independent interpolation. For vector math, see Vector2 / Vector3. For time-based control using coroutines, see Coroutine / IEnumerator.
If you find any errors or copyright issues, please contact us.