Making a camera switch in JS, how do I add more cameras?

So, here is my set-up…
I have two cameras in my scene with the Tag “MainCamera” .

One camera is a third person camera with a script of its own, it targets to the main player. It has settings for height, distance and for rotating around the player.

The other Camera is a first person camera, it 90% same code as the first camera except that some settings such as height and distance are fixed and moving the mouse on the horizontal axis rotates the player. The first person camera is set up so that it places itself inside the player’s head when it is active.

I made a script and an empty object to place the script.

I want to press the key ‘V’ to switch between the two cameras and possibly add more cameras that may be toggled with the script.

How do I add more than two cameras?

Here is the code-

#pragma strict

var cam1 : GameObject;
var cam2 : GameObject;
//var Camera3 : GameObject;

var camOn : boolean = false;


function Start () {

}

function Update () {
	
	if(Input.GetKeyDown(KeyCode.V)) {
         camOn = !camOn;
     if(camOn){
	cam1.active = true;
     cam2.active = false; }
     if(!camOn){
		cam2.active = true;
	 cam1.active = false; }
	 }
}

Hi :slight_smile:

You want to have a list of all the cameras in your game, list that you can do with GameObject.FindObjectsWithTag("Main Camera"); (note that there is a S in Objects) or you can use Unity’s feature Camera.allCameras (Unity - Scripting API: Camera.allCameras) but it takes only the enabled cameras of the scene so you’ll need to disable everything afterwards … and if you have other cameras that you don’t wanna use, you can’t. Anyway, i’d use the FindObjectsWithTag method :slight_smile:

So we need a couple variables, to store all the cameras, to store the current camera and an index to circle through the cameras. Let’s say you want to have 5 different cameras.

var allCams : GameObject[5]; //That's an array, note the square brackets in the type.
var curCam : GameObject; //Camera in use
var index : int; //Index

If you want more (or less) cameras, you need to change the 5 value in GameObject[5]. Now we need to initialize them:

function Start () {
   index = 0;
   allCams = GameObject.FindObjectsWithTag("Main Camera"); //We take the cameras we need
   curCam = allCams[index]; //We say we use first camera in the array

   //We deactivate all cams to only set active the one we want
   for (var cam in allCams){
      cam.SetActive(false);
   }
   curCam.SetActive(true);
}

Now we want to circle through the cameras if user presses V → we want to circle through the array and we’ll use index for that:

function Update () {
   if (Input.GetKeDown(KeyCode.V)){
      curCam.SetActive(false); //Curent cam is disabled
      index += 1; //We step forxward in the array

      if(index > allCams.Length) {index = 0;} //If we are out of range, go back to 0

      curCam = allCams [index]; //We pick the new camera in the array
      curCam.SetActive(true); //We activate it
   }
}

Now that you have everything you need to build your script. BUT, there are a few things you need to now:

  • I’m not very familiar with JS so it might not directly work
  • When you use FindObjectsWithTag you can’t control the order in the array so you might want to build your array differently, see Array.Push() or a public array that you’d fill in the inspector …
  • curCam is not very useful, it’s only here to store allCams[index], you can get rid of it (and use allCams[index] instead), i just find it makes the script easier to read.