How We Implemented Gravity in Our Game

Before and after

This article is about the development process of our first game. During the process, we are learning how to use Unity & Blender from scratch to create everything you see in the article. The game itself will be about growing and taking care of plants, and we hope to be able to release it in the near future.

Before we started implementing gravity for our player character, it was possible to walk straight into the sky. We had various ideas for how to solve this problem, including building a fence around the island or having the player detect edges to automatically avoid them. We settled on implementing gravity since this would make the gameplay more fun & realistic, and it would be easier to implement.

An animated image showing the player character being able to walk into the air without being affected by gravity

We knew that Unity had built-in methods for gravity so we wanted to use those rather than trying to reinvent the wheel and write a brand new method for gravity

Trial and Error

An animated picture of the player character is having a seizure on the ground instead of walking upright due to a bug

Before we started to implement gravity, the game object for the player already had a Rigidbody and a Collider. Since the beginning of development, we have used these for detecting and interacting with items.

The first thing we did to try and implement gravity was to check the box “Use Gravity” on the Rigidbody and uncheck the box “Is Trigger” on the Box Collider. As you can see in the animated image in the upper left, this did not go as well as we thought it would.

An animated image of the player character falling to the ground wobbling

We realized that the movement script interfered with the gravity of the Rigidbody, so we wanted to try disabling it to see what would happen.

As you can see in the animated image to the left, the gravity started working more as it should and the player character stopped misbehaving, so it felt like a step in the right direction.

An animated image of the player character falling over on the ground

The next problem we noticed was that the player character fell very oddly and we suspected that this was caused by the colliders being incorrectly placed. Previously we had only used the colliders to detect items, but not for physical collisions.

After moving and configuring the colliders, the character fell more naturally, so it felt like we were getting closer.

We also wanted to keep the player character from falling over, so we froze the X & Z rotation on the Rigidbody and finally, the character now fell to the ground upright as it should!

An animated image of the player character falling to the ground while standing upright

However, when we tried to enable the movement script again, the same problems of seizure-like behavior reappeared, so we had to approach the problem from another angle.

The Solution

We concluded that the movement script had to be modified to use some kind of existing gravity method instead of trying to utilize the Rigidbody gravity. After some Googling we found a great solution that checked all of the boxes that we wanted for the solution:

  • The solution was very short(only 1 line of code), which meant that the implementation was quick and easy to understand
  • It used an existing method for gravity, so we didn’t have to “reinvent the wheel”
  • It was completely compatible with our movement script, so we didn’t have to rewrite anything else

Here is the line that solved the problem:

movementOutput += Physics.gravity;

In this example, the variable movementOutput represents the accumulated movement of the character, in other words where the player is going and at what speed and all we did was to add gravity to that calculation. As you can see in the image below, adding physics to the script enabled the player character to move just the way we intended it to!

An animated image of the player character falling to the ground and using gravity

The next problem that we had to solve was that the player character was walking with his waist below the ground. This was quite easy to solve, the only thing we had to do was to move the 3D model of the player character up a bit.

An animated image of the 3D model of the player character being moved up as not to have its waist below ground
An animated image of the player character moving off the edge of the island, falling and being spawned back on the island

For the final step we also wanted to make sure that the player got back on land if they jumped off, and this also proved quite easy to solve.

All we had to do was to write a function that checked if the player got below a certain threshold if so, move them to a position on top of the island.

Here is the finished function that we used:

if (gameObject.transform.position.y <= fallingDistanceThreshold){
gameObject.transform.position = spawn.transform.position;
}

Conclusion

As with most things when creating a game, there is a solution out there and if you try hard enough you will find it!

Do you have any questions or comments about the game or how we implemented gravity?

Don’t hesitate to write a comment and we will reply to the best of our ability.

--

--

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