Switching between multiple cameras

Choosing from a variety of cameras is a common feature in many genres: race sims, sports sims, tycoon/strategy, and many others. In this recipe, we will learn how to give players the ability of choosing an option from many cameras using their keyboard.

Getting ready

In order to follow this recipe, we have prepared a package containing a basic level named basicScene. The package is in the folder 0423_02_01_02.

How to do it...

To implement switchable cameras, follow these steps:

  1. Import the basicLevel package into your Unity project.
  2. In the Project view, open basicScene from the 02_01_02 folder. This is a basic scene featuring a directional light, a camera, and some geometry.
  3. Add two more cameras to the scene. You can do it through the Create drop-down menu on top of the Hierarchy view. Rename them cam1 and cam2.
  4. Change the cam2 camera's position and rotation so it won't be identical to cam1.
  5. Create an Empty game object by navigating to Game Object | Create Empty. Then, rename it Switchboard.
  6. In the Inspector view, disable the Camera and Audio Listener components of both cam1 and cam2.
    How to do it...
  7. In the Project view, create a new C# script. 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 (){  
      	    int i = 0;
    	    for(i=0; i<cameras.Length; i++){  
    		    if (Input.GetKeyUp(shortcuts[i]))
            	    SwitchCamera(i);
            }
        }
    
        void  SwitchCamera ( int index  ){
    	   	int i = 0;
    	    for(i=0; i<cameras.Length; i++){  
    		    if(i != index){
    			    if(changeAudioListener){
    				    cameras[i].GetComponent<AudioListener>().enabled = false;
    			    }
    			    cameras[i].camera.enabled = false;
    		    } else {
    			    if(changeAudioListener){
    				    cameras[i].GetComponent<AudioListener>().enabled = true;
    			    }
    			    cameras[i].camera.enabled = true;
    		    }
    	    }
        }
    }
  9. Attach CameraSwitch to the Switchboard game object.
  10. In the Inspector view, set both Cameras and Shortcuts size to 3. Then, drag the scene cameras into the Cameras slots, and 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.

How it works...

The script is very straightforward. All it does is capture the key pressed and enable its respective camera (and its Audio Listener, in case the Change Audio Listener option is checked).

There's more...

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

Using a single-enabled camera

A different approach to the problem would be keeping all the 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 could change your camera from other game object's scripts by using a line of code, such as the one shown here:

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

See also

  • The Making an inspect camera recipe.
..................Content has been hidden....................

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