Compatibility with UnityEngine mathematics
If you want to Burst-compile your project, it's best practice to use the Unity Mathematics package by default and only use Mathf when necessary. Unity Mathematics isn't implemented in the same way as the UnityEngine.Mathf struct in the core Unity engine. Because of this, if your application relies on the specific behaviors of Mathf, you'll have to reimplement them to get similar behavior in Unity Mathematics.
Important
For performance reasons, if your project uses the Mono compiler, you should continue to use the mathematical operations in Mathf, rather than Unity Mathematics.
You can use both Mathf and Unity Mathematics methods in your project, but it might impact on the performance of your application because the conversions between the UnityEngine and Unity.Mathematics types such as Vector3 to float3 and vice-versa are performance-intensive.
Porting UnityEngine code to Unity.Mathematics
If you want to migrate code from UnityEngine to Unity.Mathematics, you need to make the following changes first:
- Update
UnityEnginetypes toUnity.Mathematicstypes. For example,Vector4tofloat4, andQuaterniontoquaternion. These examples aren't exhaustive: see the Scripting API reference for the full list of Unity Mathematics types available. - Update any operators involved in matrices or vectors. For example, the
Matrix4x4multiplication operator implements matrix multiplication, but thefloat4x4multiplication operator implements componentwise multiplication. - Degrees to radians
- How your code generates random numbers.
RandominUnity.Mathematicsworks differently toRandominUnityEngine. You can completely control random number generation withRandominUnity.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 convertUnityEnginecode which is sensitive to the bounds. For more information, see the documentation on Random numbers.