# Introduction to Unity math APIs

> Understand the different math APIs Unity provides and how they compare.

Unity provides two different sets of math APIs for math functions and structures commonly required in Unity projects:

* [Unity Engine math APIs](/engine/6000.6/manual/scripting/programming-math/unity-engine-math.md): A set of mutually compatible APIs in the `UnityEngine` namespace. This includes the [`Mathf`](/engine/6000.6/script-reference/unityengine/mathf.md) class for common math functions, including trignomometric, logarithmic, and other oprations. It also includes the [`UnityEngine.Random`](/engine/6000.6/script-reference/unityengine/random.md) class for random number generation, and classes for data structures such as [`Vector2`](/engine/6000.6/script-reference/unityengine/vector2.md), [`Vector3`](/engine/6000.6/script-reference/unityengine/vector3.md), [`Matrix4x4`](/engine/6000.6/script-reference/unityengine/matrix4x4.md), and [`Quaternion`](/engine/6000.6/script-reference/unityengine/quaternion.md).
* [Unity Mathematics APIs](/engine/6000.6/manual/scripting/programming-math/unity-mathematics.md): A set of mutually compatible APIs in the `Unity.Mathematics` namespace that provide [Burst-compilable](/engine/6000.6/manual/scripting/compilation-and-code-reload/script-compilation/burst.md) and [Single Instruction, Multiple Data (SIMD)](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data)-friendly alternatives to the `UnityEngine` math APIs. This includes the `math` static class for common math functions, the `Unity.Mathematics.Random` class for random number generation, and data structures such as [`float2`](/engine/6000.6/script-reference/unity/mathematics/float2.md), [`float3`](/engine/6000.6/script-reference/unity/mathematics/float3.md), [`float4`](/engine/6000.6/script-reference/unity/mathematics/float4.md), [`float4x4`](/engine/6000.6/script-reference/unity/mathematics/float4x4.md), and [`quaternion`](/engine/6000.6/script-reference/unity/mathematics/quaternion.md).

## Compatibility of UnityEngine math and Unity Mathematics

The `UnityEngine` and `Unity.Mathematics` APIs have important implementation differences. If your application relies on specific behaviors of one, you'll usually need to reimplement them to get equivalent behavior in the other.

You can use both `UnityEngine` and `Unity.Mathematics` APIs in your project, but it might impact the performance of your application because the conversions between the `UnityEngine` and `Unity.Mathematics` types, such as `Vector3` to `float3`, are performance-intensive.

In general, for performance reasons the following is recommended:

* In code compiled just-in-time (JIT) with the [Mono scripting backend](/engine/6000.6/manual/scripting/compilation-and-code-reload/script-compilation/backends/mono.md): use `UnityEngine` math APIs rather than `Unity.Mathematics`.
* In code compiled ahead-of-time (AOT) with [Burst](/engine/6000.6/manual/scripting/compilation-and-code-reload/script-compilation/burst.md): use Unity Mathematics by default and only use the `UnityEngine` math APIs when necessary.

## Converting between UnityEngine math and Unity.Mathematics

To migrate code between the `UnityEngine` and `Unity.Mathematics` APIs, you must do the following:

* Replace types from one API with their equivalents from the other. For example, replace usages of [`Vector4`](/engine/6000.6/script-reference/unityengine/vector4.md) with [`float4`](/engine/6000.6/script-reference/unity/mathematics/float4.md), and [`Quaternion`](/engine/6000.6/script-reference/unityengine/quaternion.md) with [`quaternion`](/engine/6000.6/script-reference/unity/mathematics/quaternion.md) if migrating from `UnityEngine` to `Unity.Mathematics` APIs.
* Update any operators involved in matrices or vectors. For example, the `Matrix4x4` multiplication operator implements matrix multiplication, but the [`float4x4`](ScriptRef:Unity.Mathematics.float4xx4) multiplication operator implements componentwise multiplication.
* Convert degrees to radians where applicable.
* Update your code's random number generation. [`Unity.Mathematics.Random`](/engine/6000.6/script-reference/unity/mathematics/random.md) works differently to [`UnityEngine.Random`](/engine/6000.6/script-reference/unityengine/random.md). You can completely control random number generation with `Random` in `Unity.Mathematics`, and it's instanced, rather than static. It's also exclusive with its upper bound, which is important to bear in mind if you want to convert `UnityEngine` code which is sensitive to the bounds. For more information, refer to [Unity Mathematics programming reference](/engine/6000.6/manual/unity-mathematics-examples.md).

The following table provides an overview of some of the key equivalent types and members in the two APIs:

| **Feature**        | **UnityEngine math**                                                                                                                                 | **Unity Mathematics**                                                                                                                                                                                                                                |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Namespace          | `UnityEngine`                                                                                                                                        | `Unity.Mathematics`                                                                                                                                                                                                                                  |
| Scalar math class  | [`Mathf`](/engine/6000.6/script-reference/unityengine/mathf.md) (static)                                                                             | [`math`](/engine/6000.6/script-reference/unity/mathematics/math.md) (static)                                                                                                                                                                         |
| Scalar type        | `float`                                                                                                                                              | `float`                                                                                                                                                                                                                                              |
| 2D Vectors         | [`Vector2`](/engine/6000.6/script-reference/unityengine/vector2.md)                                                                                  | [`float2`](/engine/6000.6/script-reference/unity/mathematics/float2.md)                                                                                                                                                                              |
| 3D Vectors         | [`Vector3`](/engine/6000.6/script-reference/unityengine/vector3.md)                                                                                  | [`float3`](/engine/6000.6/script-reference/unity/mathematics/float3.md)                                                                                                                                                                              |
| 4D Vectors         | [`Vector4`](/engine/6000.6/script-reference/unityengine/vector4.md)                                                                                  | [`float4`](/engine/6000.6/script-reference/unity/mathematics/float4.md)                                                                                                                                                                              |
| Integers           | [`Vector2Int`](/engine/6000.6/script-reference/unityengine/vector2int.md), [`Vector3Int`](/engine/6000.6/script-reference/unityengine/vector3int.md) | [`int2`](/engine/6000.6/script-reference/unity/mathematics/int2.md), [`int3`](/engine/6000.6/script-reference/unity/mathematics/int3.md), [`int4`](/engine/6000.6/script-reference/unity/mathematics/int4.md)                                        |
| Quaternions        | [`Quaternion`](/engine/6000.6/script-reference/unityengine/quaternion.md)                                                                            | [`quaternion`](/engine/6000.6/script-reference/unity/mathematics/math/quaternion.md)                                                                                                                                                                 |
| Matrices           | [`Matrix4x4`](/engine/6000.6/script-reference/unityengine/matrix4x4.md)                                                                              | [`float4x4`](/engine/6000.6/script-reference/unity/mathematics/math/float4x4.md), [`float3x3`](/engine/6000.6/script-reference/unity/mathematics/math/float3x3.md), [`float2x2`](/engine/6000.6/script-reference/unity/mathematics/math/float2x2.md) |
| Random             | [`UnityEngine.Random`](/engine/6000.6/script-reference/unityengine/random.md)                                                                        | [`Unity.Mathematics.Random`](/engine/6000.6/script-reference/unity/mathematics/random.md)                                                                                                                                                            |
| Mod                | [`Mathf.Repeat`](/engine/6000.6/script-reference/unityengine/mathf/repeat.md) or `%`                                                                 | [`math.modf`](/engine/6000.6/script-reference/unity/mathematics/math/modf.md)                                                                                                                                                                        |
| Epsilon            | [`Mathf.Epsilon`](/engine/6000.6/script-reference/unityengine/mathf/epsilon.md)                                                                      | [`math.EPSILON`](/engine/6000.6/script-reference/unity/mathematics/math/epsilon.md)                                                                                                                                                                  |
| Dot product        | [`Vector3.Dot`](/engine/6000.6/script-reference/unityengine/vector3/dot.md)                                                                          | [`math.dot`](/engine/6000.6/script-reference/unity/mathematics/math/dot.md)                                                                                                                                                                          |
| Cross product      | [`Vector3.Cross`](/engine/6000.6/script-reference/unityengine/vector3/cross.md)                                                                      | [`math.cross`](/engine/6000.6/script-reference/unity/mathematics/math/cross.md)                                                                                                                                                                      |
| Lerp (vector)      | [`Vector3.Lerp`](/engine/6000.6/script-reference/unityengine/vector3/lerp.md)                                                                        | [`math.lerp(float3, float3, t)`](/engine/6000.6/script-reference/unity/mathematics/math/lerp.md)                                                                                                                                                     |
| Slerp (quaternion) | [`Quaternion.Slerp`](/engine/6000.6/script-reference/unityengine/quaternion/slerp.md)                                                                | [`math.slerp`](/engine/6000.6/script-reference/unity/mathematics/math/slerp.md)                                                                                                                                                                      |
| Matrix multiply    | `a * b`                                                                                                                                              | [`math.mul(a, b)`](/engine/6000.6/script-reference/unity/mathematics/math/mul.md)                                                                                                                                                                    |

For a comprehensive list of equivalent functions and types, refer to the respective API references.

## Additional resources

* [Unity Engine math APIs](/engine/6000.6/manual/scripting/programming-math/unity-engine-math.md)
* [Unity Mathematics APIs](/engine/6000.6/manual/scripting/programming-math/unity-mathematics.md)
