A Basic Racetrack for our Car

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:

Rescaling a Plane via the Inspector

You can set the same values for the Position of your plane to the ones we have in the image above:

Position

X: 0
Y: -0.3
Z: 0

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:

Using a cube as a border

It's the Position and Scale values you need to change. To these:

Position

X: 20
Y: 0.2
Z: -0.2

Scale

X: 1
Y: 1
Z: 80

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:

The Move and Scale icons in 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:

Position

X: -20
Y: 0.2
Z: -0.2

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:

Position

X: 0
Y: 0.2
Z: 40

Scale

X: 1
Y: 1
Z: 40

Your Inspector should look like this:

The Inspector with some Transform values changed

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:

Position

X: 0
Y: 0.2
Z: -40

Here's the Inspector for Cube4:

Completing the racetrack border

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.

Menu showing the Create Empty item selected

Rename this empty object. Call it TrackBorders. Now, in the Hierarchy, drag all of your Cube borders onto the empty TrackBorders object:

The Hierarchy with four game objects selected

When you drag all the cubes onto TrackBorders, they should be nicely grouped:

Dragging and dfopping four items onto an empty game object to create a parent/child relationship

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):

Position

X: -0.8
Y: 0.2
Z: 0

Scale

X: 25
Y: 2
Z: 60

Here's the Inspector:

The Transform values for the center of the racetrack

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:

Scene view showing the basic racetrack

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.

 

Rigidbody

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:

The Add Component menu showing the Physics item highlighted

From the Physics items, choose Rigidbody:

The Add Component menu showing the Rigidbody item highlighted

Or you can type into the search box at the top:

Searching via the menu for the Rigidbody component

When you click on Rigidbody, you'll see a new component added to your car:

A Rigidbody component added to a game object

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:

Scale

X: 4
Y: 1
Z: 1

Set the position to these values:

Position:

X: 17.5
Y: 0.2
Z: 12

The Inspector should look like this (we've renamed our cube to ChicaneA):

The Transform values for a cube used as a chicane

You can create a new material, at this point and colour your new cube.

Now duplicate your cube. Move it these positions:

Position:

X: 17.5
Y: 0.2
Z: -5

It's just movement on the Z Position to -5 from 12. X and Y are the same.

Here's the Inspector:

A second cube used as a chicane and its Transform values

Duplicate the cube a third time and change the position to these values:

Position:

X: 13.5
Y: 0.2
Z: 3.5

And here's the Inspector for your third cube:

The Transform values for a third chicane

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:

All the chicanes grouped in the Hierarchy

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.

<--Back to the Unity 3D Course Contents Page