Adding Gravity in Unity

Make the player be attracted to the floor.

Rusben Guzman
Jan 14, 2022

Objective: add gravity to the game through script.

We have a player able to move but he is static in the Y axis and that is not the behavior we want. In this case we are going to add gravity through scripting.

In the player script I created a new variable for the gravity value. Then in the update method now I check if the player is grounded, if it is false so we are going to apply the gravity by subtracting the value of the gravity variable to the velocity vector in the Y axis.

And that’s it!!! Now the player starts falling when is not grounded.

--

--

Rusben Guzman
Rusben Guzman

Written by Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

No responses yet