Unity - Multiple Cameras

Driving games usually let you switch cameras. One camera might be behind the car, another a front view, while a third might be a top down view. Let's see how to implement that. It's quite easy!

The only camera in the scene is the Main Camera currently attached to the car. But you can add as many cameras as you need. So, right click on CAR-ROOT. From the menu that appears, select Camera. Rename the camera to Cam2 (the Main camera can be camera 1). Change the Y and Z positions so that the camera is at the front. We went for a Y value of 0.9 and a Z value of 1.5. Which gave us this view:

Camera at the front of a car

Repeat the process and add a third camera. Call it Cam3. Position this camera so that it has an overhead view. We went for these values:

Position

X: 0
Y: 6
Z: 0.3

Rotation

X: 90
Y: 0
Z: 0

The values gave us a view like this:

Camera pointing down at the car

Our Main Camera gives us a view like this:

Camera at the rear of a car

What we want to do is to switch cameras when a number key on the top row of the keyboard is pressed. Pressing 1 will be the main view, pressing 2 will switch to the front of the car, and pressing 3 will be the top-down view.

In the Hierarchy, create a new empty game object. Call it Cams. In the Inspector on the right, click the Add Component button. Add a new script. Call it CamControl. Open the script up. Add these three variables inside of the class:

public GameObject cam1;
public GameObject cam2;
public GameObject cam3;

You can delete the Start method because you don't need it. In the Update method, add this if statement:

if (Input.GetKeyDown(KeyCode.Alpha1)) {

cam1.SetActive(true);
cam2.SetActive(false);
cam3.SetActive(false);

}

Simple enough. The if statement checks for a key down event. The KeyCode is Alpha1. This gets you the number 1 on the top row of your keyboard, rather than the number 1 on your num pad, if you even have one. If a 1 is detected then we set the active status of cam2 and cam3 to false and cam1 to true.

Now add two more similar if statements. These:

if (Input.GetKeyDown(KeyCode.Alpha2))
{

cam1.SetActive(false);
cam2.SetActive(true);
cam3.SetActive(false);

}

if (Input.GetKeyDown(KeyCode.Alpha3)) {

cam1.SetActive(false);
cam2.SetActive(false);
cam3.SetActive(true);

}

Now we check for Alpha2 and Alpha3, setting the relevant cams to true or false. Your code should look like this:

Unity C# code to switch cameras when a number is pressed on the keyboard

Save your work and go back to Unity. Drag and drop the various cameras from the Hierarchy onto the slots in the Inspector. The Inspector should look like this, when you're done:

In the Hierarchy, select your Cam2 object. Now uncheck the box at the top in the Inspector:

Unchecking the box sets the active status to false.

Do the same for your Cam3 object: uncheck the box to deactivate the camera.

Play your game and try it out. Press the 1, 2, or 3 keys on your keyboard. Your camera action should look the one in this short video below:

 

One last thing. If your car is handling like a dog, there are a few settings you can tweak. Click on your CAR-ROOT in the Hierarchy. Expand the Rigidbody section. Play around with the Mass and the Drag. Increase the mass of the car to make it not so flighty. The Drag value goes from 0 to 1. A higher value will see your car go slower, but the steering will be easier. A value of 0.1 seems to work OK.

Also expand your script. Adjust the Max Torque value and the Max Steer Angle to see what they do and the effect they have on your car.

You could also take a look at the Wheel Colliders you added (CAR-ROOT > WHEELS). There's a lot of settings to play around with here and they can have a dramatic effect on the way your car performs.

 

In the next lesson below, we'll add a speedometer to the car.

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