Switching between multiple cameras

Choosing from a variety of cameras is a common feature in many genres: racing, sports, tycoon/strategy, and many others. In this recipe, you will learn how to give players the ability to choose from many cameras by using their keyboards.

Getting ready

For this recipe, we have prepared the BasicScene Unity package containing a scene named BasicScene. The package is in the 1362_05_codes folder.

How to do it...

To implement switchable cameras, follow these steps:

  1. Import the BasicScene package into a new Project.
  2. From the Project view, open the BasicScene level. This is a basic scene featuring an animated character and some extra geometry.
  3. Add two more cameras to the scene through the Create drop-down menu on top of the Hierarchy view (Create | Camera). Rename them cam1 and cam2.
  4. Change the cam2 camera's position and rotation so that it won't be identical to cam1.
  5. Create an Empty GameObject by navigating to the Create drop-down menu on top of the Hierarchy view (Create | Create Empty). Then, rename it Switchboard.
  6. From the Inspector view, disable the Camera and Audio Listener components of both cam1 and cam2. Also, set their Tags as MainCamera, as shown:
    How to do it...
  7. From the Project view, create a new C# Script file. Rename it CameraSwitch and open it in your editor.
  8. Open your script and replace everything with the following code:
    using UnityEngine;
    
    public class CameraSwitch : MonoBehaviour {
      public GameObject[] cameras;
      public string[] shortcuts;
      public bool  changeAudioListener = true;
      void  Update (){
        if (Input.anyKeyDown) {
          for (int i=0; i<cameras.Length; i++) {
            if (Input.GetKeyDown (shortcuts [i]))
              SwitchCamera (i);
          }
        }
      }
    
    void  SwitchCamera (int indexToSelect){
      for (int i = 0; i<cameras.Length; i++){
        // test whether current array index matches camera to make active
          bool cameraActive = (i == indexToSelect);
          cameras[i].GetComponent<Camera>().enabled = cameraActive;
    
        if (changeAudioListener)
              cameras[i].GetComponent<AudioListener>().enabled = cameraActive;
          }
        }
    }
  9. Attach CameraSwitch to the Switchboard GameObject.
  10. From the Inspector view, set both the Cameras and Shortcuts sizes to 3. Then, drag and populate the Cameras slots with the cameras from the scene (including the Main Camera, within the Multipurpose Camera Rig | Pivot GameObject) Then, type 1, 2, and 3 into the Shortcuts text fields, as shown in the next screenshot:
    How to do it...
  11. Play your scene and test your cameras by pressing 1, 2, and 3 on the keyboard.

How it works...

The script is very straightforward. First, it compares the key being pressed to the list of shortcuts. If the key is indeed included on a list of shortcuts, it is passed on to the SwitchCamera function, which, in turn, goes through a list of cameras, enables the one associated with the shortcut that was received, and also enables its Audio Listener, in case the Change Audio Listener option is checked.

There's more...

Here are some ideas about how you could try twisting this recipe a bit.

Using a single-enabled camera

A different approach to the problem would be keeping all secondary cameras disabled and assigning their position and rotation to the main camera via a script (you would need to make a copy of the main camera and add it to the list, in case you wanted to save its Transform settings).

Triggering the switch from other events

Also, you can change your camera from other GameObjects' scripts by using a line of code such as the one given here:

GameObject.Find("Switchboard").GetComponent("CameraSwitch").SwitchCamera(1);

See also

  • The Creating an in-game surveillance camera recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.171.125