Introduction to Unity math APIs
Understand the different math APIs Unity provides and how they compare.
Read time 3 minutesLast updated 6 days ago
Unity provides two different sets of math APIs for math functions and structures commonly required in Unity projects:
- Unity Engine math APIs: A set of mutually compatible APIs in the namespace. This includes the
UnityEngineclass for common math functions, including trignomometric, logarithmic, and other oprations. It also includes theMathfclass for random number generation, and classes for data structures such asUnityEngine.Random,Vector2,Vector3, andMatrix4x4.Quaternion - Unity Mathematics APIs: A set of mutually compatible APIs in the namespace that provide Burst-compilable and Single Instruction, Multiple Data (SIMD)-friendly alternatives to the
Unity.Mathematicsmath APIs. This includes theUnityEnginestatic class for common math functions, themathclass for random number generation, and data structures such asUnity.Mathematics.Random,float2,float3,float4, andfloat4x4.quaternion
Compatibility of UnityEngine math and Unity Mathematics
The and 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.
UnityEngineUnity.MathematicsYou can use both and APIs in your project, but it might impact the performance of your application because the conversions between the and types, such as to , are performance-intensive.
UnityEngineUnity.MathematicsUnityEngineUnity.MathematicsVector3float3In general, for performance reasons the following is recommended:
- In code compiled just-in-time (JIT) with the Mono scripting backend: use math APIs rather than
UnityEngine.Unity.Mathematics - In code compiled ahead-of-time (AOT) with Burst: use Unity Mathematics by default and only use the math APIs when necessary.
UnityEngine
Converting between UnityEngine math and Unity.Mathematics
To migrate code between the and APIs, you must do the following:
UnityEngineUnity.Mathematics- Replace types from one API with their equivalents from the other. For example, replace usages of with
Vector4, andfloat4withQuaternionif migrating fromquaterniontoUnityEngineAPIs.Unity.Mathematics - Update any operators involved in matrices or vectors. For example, the multiplication operator implements matrix multiplication, but the
Matrix4x4multiplication operator implements componentwise multiplication.float4x4 - Convert degrees to radians where applicable.
- Update your code's random number generation. works differently to
Unity.Mathematics.Random. You can completely control random number generation withUnityEngine.RandominRandom, 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 convertUnity.Mathematicscode which is sensitive to the bounds. For more information, refer to Unity Mathematics programming reference.UnityEngine
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 | | |
| Scalar math class | | |
| Scalar type | | |
| 2D Vectors | | |
| 3D Vectors | | |
| 4D Vectors | | |
| Integers | | |
| Quaternions | | |
| Matrices | | |
| Random | | |
| Mod | | |
| Epsilon | | |
| Dot product | | |
| Cross product | | |
| Lerp (vector) | | |
| Slerp (quaternion) | | |
| Matrix multiply | | |
For a comprehensive list of equivalent functions and types, refer to the respective API references.