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(){
-
if(Input.GetKey("1")){
-
Debug.Log("Using Camera One");
-
camSwap(1);
-
}
-
}
-
function camSwap(currentCam : int){
-
-
}
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){
-
var cameras = GameObject.FindGameObjectsWithTag("cam");
-
-
for (var cams : GameObject in cameras){
-
cams.GetComponent(Camera).enabled = false;
-
}
-
}
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 -
-
var oneToUse : String = "Camera"+currentCam;
-
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.
Javascript
-
function Update () {
-
if(Input.GetKey("1")){
-
Debug.Log("Using Camera One");
-
camSwap(1);
-
}
-
if(Input.GetKey("2")){
-
Debug.Log("Using Camera Two");
-
camSwap(2);
-
}
-
if(Input.GetKey("3")){
-
Debug.Log("Using Camera Three");
-
camSwap(3);
-
}
-
}
-
-
function camSwap(currentCam : int){
-
var cameras = GameObject.FindGameObjectsWithTag("cam");
-
-
for (var cams : GameObject in cameras){
-
cams.GetComponent(Camera).enabled = false;
-
}
-
-
var oneToUse : String = "Camera"+currentCam;
-
gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
-
}
C# Equivalent
-
using UnityEngine;
-
using System.Collections;
-
-
public class camControl : MonoBehaviour {
-
-
void Update () {
-
if(Input.GetKey("1")){
-
Debug.Log("Using Camera One");
-
camSwap(1);
-
}
-
if(Input.GetKey("2")){
-
Debug.Log("Using Camera Two");
-
camSwap(2);
-
}
-
if(Input.GetKey("3")){
-
Debug.Log("Using Camera Three");
-
camSwap(3);
-
}
-
}
-
-
void camSwap(int currentCam){
-
GameObject[] cameras = GameObject.FindGameObjectsWithTag("cam");
-
-
foreach (GameObject cams in cameras){
-
Camera theCam = cams.GetComponent<Camera>() as Camera;
-
theCam.enabled = false;
-
}
-
-
string oneToUse = "Camera"+currentCam;
-
Camera usedCam = GameObject.Find(oneToUse).GetComponent<Camera>() as Camera;
-
usedCam.enabled = true;
-
}
-
}