Unity 3D – Playing Maths with Unity 3D

If you like Maths and Physics, you can explore your passion with Unity !

Unity 3D is a Game Engine. It provides Physic laws abstraction through the NVIDIA physiX physics engine.

It modelises gravity, objects collisions, forces, frictions.
This is tightly coupled with Unity 3D Math library.

I will explain the most used Math concepts.

Vectors 

Vector class is a representation of vectors. All basic operations are available.

I will give some examples of how this can be used in practice :

– to see if an object is coming towards you or not, use Vector3.dot
– to see how intense will be the bounce with a wall analyse cross magnitude 

Lerping / Slerping

Lerp stands for linear interpolation, you specify :

– An initial position
– A final position
– A fraction of the path between the two positions [0-1]

In practice you use this function by peeking the fraction to be relative to elapsed time e.g. N * time.deltatime.

Time

There is different time units in Unity 3D, those relative to the device it’s running on and fix time units :

– Time.fixedtime (see Player settings)
– Time.time
– Time.deltatime represents the amount of time between two frames and is relative to the device rate.

Tip : in Multiplayer lobbies, you should not use Unity time units but the Multiplayer engine time unit (e.g. photon network.time, unity network.time )

Instantiation

In Unity you can instantiate Game objects dynamically by using the Instantiate function. First provide the game object prefab to instantiate then it’s positions and Rotation. Always set Rotation to Quatzrnion.identity to preserve default Rotation.

Translation / Rotation

Translations simply use 3D position. There’s a local position and a global one.

Unity represents objects Rotation in space using the Maths concept of Quaternion. That is a Vector4.

Forces

You can apply forces to rigid bodies through the Add Forces function. It takes into account the weight of the game object.

Raycasts

Similar to a wave, Raycasts are used to represent the trajectory of an object moving along a given line in space (it’s velocity Vector). You can then determinate which game object you are gonna hit in real time through a callback and his hit game object.

That’s all ! Of course you have access to standard Math functions.

Please don’t hesitate to talk about some concepts I could have forget in the comment section πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *