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.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

It’s the 5th Birthday of Feren OS! (and Feren OS July 2020 Snapshot is now available!)

Bounty Hacker Write-up

How to set up MetaMask for Polygon

How to install an SSL Certificate on FileMaker Server?

Quantum Error Ignoration

Accident Lawyer Sandy Utah

accident lawyer sandy utah

Supercharge your Confluence searches with ScriptRunner

The Fourth Implementation of the Community-centric Delegation Campaign & the Second One-year Token…

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.

More from Medium

The Escape Button is a Feature

Unity Interface Layout

Level Design in Unity 3D Part 1: The Floor

Adding a health powerup to our prototype