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

Programmer jokes #8

SlinkyFinance NFT FARM

Simple CRUD application in laravel 5.8 Part-1

Flashing ROM using SP Flash Tool

Simple Things That Makes Your Code Look Better

SQL From Zero to Hero in 30 Days — Day 3 Filtering

Installing a KubeFlow on your Local Machine

Automate AMI creation for EC2 Instances Using Systems Manager | Fit-DevOps

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

Creating a Pressure Pad in Unity

Using Height Maps and Pixel Displacements in Unity

Modular Health System in Unity!

Making a 2.5D Character Controller in Unity Part 1