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 –

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

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 –

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

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.

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

C# Equivalent

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

Share via Social Media

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

31 Comments