Adding Gravity in Unity

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rusben Guzman

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.