Working with force and gravity in Unity
3D Game Design with Unity
🕑 This lesson will take about 25 minutes
In this lesson, you will learn how to work with force and gravity in a 3D game in Unity. As an example, we will create a simple rolling ball game using the Rigidbody physics component in Unity to add force and gravity. In this game, we will use the W,A,S,D keys to move the ball left and right, and backwards and forwards. We will also use the Space key to make the ball jump up and gravity will bring the ball back down, as seen in the animated GIF below.
Rolling ball using the AddForce() function.
Watch the video below to see how you can create a simple rolling ball game with force and gravity. Then scroll down to take a look at the sample code.
Is YouTube blocked at school? Watch on Google Drive.
Here is the sample code in C# for this game.
This a simple game with no scoring or other objects to interact with, but you could easily extend the game by adding a user interface, scoring, different cameras, and objects to interact with. As an example, you could make a simple pinball game.
Next lesson: Accessing other objects and modifying their components