Introduction to Physics in Unity
By default a GameObject in Unity is only an asset in the middle of a scene without any behavior. Some methods like Translate allow us to move the player but what happens when we want to simulate aspects of the real world like gravity, forces, dynamics? Do we have to code all of these? (SPOILER ALERT: no).
To add Physics to our GameObjects we have to add a component called Rigidbody. This component provides methods to control the player movement through physics simulations.
Adding Rigidbody to a GameObject.
Ok let’s create 2 cubes, the player and one platform.
We’re gonna modify the scale of the platform to create the floor.
Ok at this point we only have 2 objects with any behaviour.
It’s time to add a rigidbody to Our player.To do this we have to select the player in the hierarchy, go to the inspector window and add a new component. Important thing, in this case you have to select the “Rigidbody” component and not the “Rigidbody2D”, the difference between them is in the name one is for 3D objects and the one with 2D in the name is for 2D objects.
Once we do it when we run the game you can see how the player is affected by the gravity.
We can modify the values in the inspector window to adapt the physics for our game, for example I increase the drag to slow down our object.
There are a hundred possibilities with the physics engine you just have to experiment and create a new world under your rules. Below I gonna leave a link with the official documentation to use Rigidbody: