unity 3d student

Switching Cameras at runtime

switching cameras in Unity
Hi all I’ve had a few requests for this and i’m currently away from my video recording rig for the festive holidays so here’s a quick written example!

In order to switch cameras you’ll simply be disabling one and enabling another. In this example, I have created a simple scene with a cube, and 3 cameras. To identify these objects and run through them, instead of finding each name in turn, we can use tagging to select the camera objects all at once in a list, then use a for loop to iterate through the list of them, finding the camera component and disabling them all, then enable the required camera. Let’s break that down a bit more simply –

I. Create a keypress that calls a custom function with an argument of an integer number, so the corresponding key can be assigned to which numbered camera to select, for example –

  1. function Update(){
  2.    if(Input.GetKey("1")){
  3.   Debug.Log("Using Camera One");
  4.   camSwap(1);
  5.    }
  6. }
  7. function camSwap(currentCam : int){
  8.  
  9. }

II. Then within the camSwap function, create a For loop to find all objects with a particular tag – in my example I made a tag called ‘cam’ and applied it to all 3 camera objects – and iterate through them, disabling the Camera components in each using GetComponent –

  1. function camSwap(currentCam : int){
  2.  var cameras = GameObject.FindGameObjectsWithTag("cam");
  3.  
  4.  for (var cams : GameObject in cameras){
  5.   cams.GetComponent(Camera).enabled = false;
  6.  }
  7. }

III. Finally, after the for loop, select the camera you DO want and enable it by creating a string of text consisting of the word Camera and the number from the currentCam argument in it*, and then using Find and GetComponent to select the object, then enable its Camera component –

  1. var oneToUse : String = "Camera"+currentCam;
  2.  gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;

* remember that for this to work, you’ll need to name your cameras Camera1, Camera2, Camera3 and so on.

Here is a full look at the script, and also refer back to the image at the top to understand how i’ve setup my objects and tagged them.

  1. function Update () {
  2.  if(Input.GetKey("1")){
  3.   Debug.Log("Using Camera One");
  4.   camSwap(1);
  5.  }
  6.  if(Input.GetKey("2")){
  7.   Debug.Log("Using Camera Two");
  8.   camSwap(2);
  9.  }
  10.  if(Input.GetKey("3")){
  11.   Debug.Log("Using Camera Three");
  12.   camSwap(3);
  13.  }
  14. }
  15.  
  16. function camSwap(currentCam : int){
  17.  var cameras = GameObject.FindGameObjectsWithTag("cam");
  18.  
  19.  for (var cams : GameObject in cameras){
  20.   cams.GetComponent(Camera).enabled = false;
  21.  }  
  22.  
  23.  var oneToUse : String = "Camera"+currentCam;
  24.  gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
  25. }

C# Equivalent

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class camControl : MonoBehaviour {
  5.  
  6.  void Update () {
  7.   if(Input.GetKey("1")){
  8.    Debug.Log("Using Camera One");
  9.    camSwap(1);
  10.   }
  11.   if(Input.GetKey("2")){
  12.    Debug.Log("Using Camera Two");
  13.    camSwap(2);
  14.   }
  15.   if(Input.GetKey("3")){
  16.    Debug.Log("Using Camera Three");
  17.    camSwap(3);
  18.   }
  19.  }
  20.  
  21.  void camSwap(int currentCam){
  22.   GameObject[] cameras = GameObject.FindGameObjectsWithTag("cam");
  23.  
  24.   foreach (GameObject cams in cameras){
  25.    Camera theCam = cams.GetComponent<Camera>() as Camera;
  26.    theCam.enabled = false;
  27.   }  
  28.  
  29.   string oneToUse = "Camera"+currentCam;
  30.   Camera usedCam = GameObject.Find(oneToUse).GetComponent<Camera>() as Camera;
  31.   usedCam.enabled = true;
  32.  }
  33. }

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

31 Comments