For your VR game to feel real, its objects should move like they would in real life. In this tutorial, you will learn how to make your game objects respect the laws of physics. If you are a beginner and want to learn how to build virtual reality games, check out our Unity 3D course!
Create a cube in the Hierarchy. We will apply physics to Cube so that it respects gravity. Move Cube up to a Y position of about 4.
By default, Cube does NOT respect gravity. Cube will remain suspended in the air – it won’t fall.
To prove it, press the Play button above the Scene window. The editor will darken. The Game window will open. The Play, Pause, and Forward buttons will become blue, meaning that the scene is live. The particles will jump and fall from Cube, but Cube will remain stationary.
Cube will not fall because we need to specifically tell Unity that we want Cube to respect the laws of physics. Press the Play button again to stop playing the game. In the Hierarchy, select Cube. We will give Cube a new component in the Inspector.
The Rigidbody Component
Click on the “Add Component” button at the bottom of the Inspector. Type “rigidbody” in the search bar. Select “Rigidbody”.
A Rigidbody component will appear in the Inspector. Rigidbody contains properties that pertain to physics. By creating this component for Cube, we have made Cube respect the laws of physics. Press the Play button. Cube will fall to the floor.
Currently, the particles fall to the left. Let’s have them fall from the bottom of the cube instead. Stop playing the scene. Select Particle System in the Hierarchy. In the Inspector, change the X Rotation value to 90. Press Play. The cube will fall, and particles will tumble out of it.
Stop playing the scene. Change the cube’s Rotation values to 45 45 40. Press Play to see the cube fall at an angle. When the cube hits the floor, it will rotate to sit on the floor.
Let’s make the cube collide with another object, such as a sphere. Stop playing the scene. In the Hierarchy, create a sphere. Position Sphere above the floor and under the cube, as in the following image. The sphere’s Position values should be about 0.2 5 0.14.
Press Play to see Cube fall and bounce against Sphere, as in the following image.
Stop playing the scene. Create two more spheres. Position them under Cube, as in the following image. To duplicate a sphere, press Control/Command+D. Alternatively, right-click, and select “Duplicate” in the Hierarchy.
Press Play to see Cube fall and tumble against the spheres.
Saving Your Project
In the Hierarchy, the file named “Untitled” contains the game’s elements. As evident in the following image, there is an asterisk beside “Untitled”. This means the file is not saved.
To save the scene, go to File > Save Scene. Give the file a name such as “Game.unity”. “Untitled” will change to “Game”. A file named “Game” will appear in the Assets folder.
To learn more about Unity, check out our Unity 3D course, where you build 30 virtual reality games!
- Writer of blog posts and books at Mammoth Interactive. Can also be found posting to Mammoth Interactive's social media.