Skip to content

Latest commit

 

History

History
93 lines (47 loc) · 4.7 KB

File metadata and controls

93 lines (47 loc) · 4.7 KB

Creating Basic 3D Objects

Session Video.

GameObjects are the basic building blocks of any Unity scene.

Creating GameObjects

Using the Hierarchy window's create tab (available via a right click), select 3D Object - Plane. You should get something like Figure 1, below.

A Plane

Figure 1: A plane

This plane is a GameObject and should appear as the object Plane in your Hierarchy view, as per Figure 2.

Hierarchy

Figure 2: Plane in the Hierarchy

Now use the same method to put another of Unity's basic GameObjects on the plane - a cube. You should get something like Figure 3:

Cube

Figure 3: A cube

At this point, you have a white cube embedded in a white plane, because both the cube and the plane have the same origin. Select the cube in the Hierarchy menu and you can see its position in the Transform component in the Inspector window. Change the Y position to 2. It should now float above the plane, just like Figure 4.

Cube

Figure 4: A cube above a plane

Adding Materials

To add some colour to the cube we need to add a material. In the Project view, click the create menu and select Material. A grey sphere should appear at the bottom of your Inspector view, just like Figure 5.

New Material

Figure 5: New material

Towards the top of the Inspector window is a colour chooser next to the word Albedo, which is the property that controls the base color and transparency of the material. Click on the white rectangle and choose a bright red colour (RGB, 255,0,0), as per Figure 6. You should now see a red sphere called New Material in the assets folder in the project view. Drag this material onto the cube in the Scene view.

Apply Material

Figure 6: Apply material

Save your scene - File > Save.

Add Physics

Now choose play in the Toolbar, and the running game will appear.

The cube just floats above the plane because, currently, there are no forces acting on the scene so nothing will move. Click on the play button again to exit game view.

To make GameObjects move in Unity, you need to add some physics.

Add a Rigidbody component to your cube (in the Inspector, add component, physics, rigidbody).

Now push play and the cube will drop under the influence of gravity (until it hits the plane). Play around with the following component options:

  1. Turn off the BoxCollider on the cube or the MeshCollider on the plane. What happened and why?

  2. Re-enable the BoxCollider on the cube and try tilting the plane by setting the plane's x and z rotation values to 2. The friction in the Rigidbody will stop the cube from sliding. What happens if you exit play then set the values to 20 rather than 2? Has your plane disappeared? At this point, you may wish to go and research Backface Culling. Play around with the values and make the plane reappear.

  3. Delete the cube and add a sphere with a y transform of 3. Now the sphere should hit the plane and roll off.

  4. Try adding a cube that the sphere will hit to deflect its movement. Note you can change the dimension of the cube to make it easier to hit.

If you've followed everything above, you should have something somewhat similar to Figure 7 (all the objects have had coloured matertials added to make them easier to see).

Figure 7: Coloured objects

However, you won't be needing the cube, so delete it (there are numerous ways to do that - quite how is left to you to figure out). Then, rename the plane "Ground", and the sphere "ball". Finally, reset the ground's transform so it's rotation values are all zero; you should have something that looks similar to Figure 8, below.

Ball and Plane

Figure 8: Ground and ball

Add a RigidBody to your ball. When you press play the ball should drop on to the ground and stop.

Add a Physics Material

In the Project view, right click and select create > Physic Material. Set the Bounciness of the material to 1, then drag the Physic Material onto the ball. You should see the Physic Material assigned to the Material property of the ball's Sphere Collider in the Inspector, as per Figure 9. Press play, and your ball should bounce.

Ball Physics Material

Figure 9: Ball Physics Material

It is a good idea to keep your project well organised, so in the Project view, right click, create > Folder, and call it "Materials". Drag your materials into that folder.

Finally, save your project.