In this lesson, you'll learn how to create a basic racetrack for your car to zoom around. The lesson is designed to give you even more practice with game objects in Unity. Let's get started.
The first thing to do is to extend the plane the car sits on. At the moment, it's 4 units big for X, Y and Z. Increase the Z value for Scale to 8 so that your plane stretches out in front of the car:
You can set the same values for the Position of your plane to the ones we have in the image above:
Now let's have some barriers around the sides of our plane.
From the menu at the top of Unity, click on the Game Object menu item. From the Game Object menu, select 3D Object > Cube. (You can also right-click in the Hierarchy and select 3D Object > Cube from the menu that appears.)
If your plane is in the same location as ours, change the Transform values for your new cube to the ones below:
It's the Position and Scale values you need to change. To these:
If your plane is not at the same location as ours, then you'll need to scale and position your new cube so that it's on the left edge of your plane. Use these tools in the toolbar at the top of Unity:
With your first cube in position, select it in the Hierarchy. Now duplicate it (CTRL + D is a shortcut.) Rename the cube to Cube2. The new cube will be on top of the old one. So, with Cube2 selected in the Hierarchy, use the red X arrow in the Scene to move it to the right border. Or you can just type the values into the Position text boxes in the Inspector. These values:
With your Cube2 still selected, duplicate it. This will create a new cube on top of Cube2. Rename the new cube to Cube3.
With Cube3 selected, type a value of 90 into the Y Rotation text box in the Inspector. Now change the Position and Scale values to these:
Your Inspector should look like this:
Duplicate Cube3 and rename it to Cube4. Now use the blue Z axis arrow to position Cube4 at the end of the plane. The values for Position should be these:
Here's the Inspector for Cube4:
To do a bit of housekeeping, we can group all four borders. Click on Game Object on the menu at the top of Unity. From the Game Object menu, select Create Empty.
Rename this empty object. Call it TrackBorders. Now, in the Hierarchy, drag all of your Cube borders onto the empty TrackBorders object:
When you drag all the cubes onto TrackBorders, they should be nicely grouped:
Now, you can click the arrow to the left of TrackBorders to contract or expand the item.
If you play your game, you should see something like ours in the short video below: (19 seconds)
(Don't worry if you've discovered that you can drive straight through
the borders. We'll fix that shortly.)
Let's have a giant cube in the middle that we can driver around.
Select your car in the Hierarchy. Move it to near the borders somewhere. Now sdd a new cube to your scene. Give it the name TrackMiddle. For the Position, and Scale, change them to these values (leave rotation on 0 for X, Y and Z):
Here's the Inspector:
Create a new material for your middle cube. Apply it to your new TrackMiddle. We've went for green, in the image above. (You can also create a material for your borders, and give at a color, you like. We'll leave ours on grey.) It should look something like in Scene view:
Now play your game. Drive your car up to the track middle and try to bump into it. What you should find is that your car can drive straight through it. It can drive straight through the borders, as well. That's because the car has no physics attached to it. Let's fix that.
There is a physics component you can attached to game objects called Rigidbody. This is used when you want collision detection. If the car and the cube both have box colliders, then adding a Rigidbody to the car will ensure that it will crash into the cubes, rather than going through them.
To add a Rigidbody to the car, first select your car, either in the Hierarchy or in the Scene. At the bottom of the Inspector, click the Add Component button. If you see a Physics menu, click that:
From the Physics items, choose Rigidbody:
Or you can type into the search box at the top:
When you click on Rigidbody, you'll see a new component added to your car:
As well as items for Mass, Drag, Angular Drag, and Use Gravity, there is an area at the bottom for Constraints. Expand this item and check all the Freeze Rotation ones. Also, check the Y box for Freeze Position.
Try your game again. When you drive around, you should be able to crash into the barriers and the cube in the middle, as in the short video below (11 seconds):
Now let's add some cubes as a chicane, to make the game a little more interesting.
Create a new empty object, just like you did with the track borders. Rename the empty object ChicaneOne. With ChicaneOne selected, create a new 3D Cube object (you should know how to do this by now). You can rename the cube, if you like. Set the Scale of your new Cube to these values:
Set the position to these values:
The Inspector should look like this (we've renamed our cube to ChicaneA):
You can create a new material, at this point and colour your new cube.
Now duplicate your cube. Move it these positions:
It's just movement on the Z Position to -5 from 12. X and Y are the same.
Here's the Inspector:
Duplicate the cube a third time and change the position to these values:
And here's the Inspector for your third cube:
Test it out. Play your game and drive around the chicanes. You should see something like this, in the short video below: (14 seconds)
Stop the game and go back to Scene view. In the Hierarchy, move all of your chicanes into ChicaneOne so they are children and ChicaneOne is the parent (you did this with the borders).
Now select your ChicaneOne group and duplicate it. Rename the group
to ChicaneTwo. The Hierarchy should look like this:
Now move ChicaneTwo across to the other side of the track. You'll need to set the Scale of ChicaneC to 3, instead of 4. And play around with the Positions of all three clocks in the ChicaneTwo group.
Before testing it out, click on your Car to select it. Now rotate your car 90 degrees on the Y axis so that it's facing the right way. (You should know how to rotate game objects by now.)
Test it out. Play your game and do a full lap, as in the video below: (27 seconds)
OK, clearly, we need a lap timer! This is where we get coding again. We'll do that in the next lesson below.