How do I switch between 3 cameras using only the right mouse button??

Hi,

I’m trying to switch between 3 cameras in a loop so it switches to the next camera on the right mouse button. At the moment I can switch between cameras using keyDown but that is not hat i’m after. I am also trying to disable everything attached to the camera not in use.

At the moment I have basic some basic script:

var camera1 : Camera;
 
var camera2 : Camera;
 
function Start(){

	camera1.enabled=true;
	camera2.enabled=false;

}

function Update(){
	
	if(Input.GetKeyDown("1")){
	camera1.enabled=true;
	camera2.enabled=false;
	
}

if(Input.GetKeyDown("2")){
	camera1.enabled=false;
	camera2.enabled=true;
	
}

}

Here’s another way to do this using an array, so IF you end up having more cameras in another scene, you won’t need to code them in. This will grab all the cameras in your scene and cycle through them as you right click.

JavaScript(UnityScript) Example:

#pragma strict

var cams : Camera[];
var current : Camera;
var index : int;
var first : int;
var last : int;

function Awake()
{
	cams = GameObject.FindObjectsOfType(Camera);
	last = (cams.Length - 1);
}

function Update()
{
	if(Input.GetMouseButtonDown(1))
	{
		if(index > last)
			index = first;
		Camera.SetupCurrent(cams[index]);
		for(var i : int = 0; i < cams.Length; i++)
		{
			if(cams *== Camera.current)*

_ cams*.enabled = true;_
_
else*_
_ cams*.enabled = false;
}
index++;
print (Camera.current.gameObject.name + " Index = " + index);
}
}*
C# Example
using UnityEngine;
using System.Collections;_

public class Example : MonoBehaviour
{
* Camera[] cams;*
* Camera current;*
* int index;*
* int first;*
* int last;*

* void Awake()*
* {*
* cams = (Camera[])GameObject.FindObjectsOfType(typeof(Camera));*
* last = (cams.Length - 1);*
* }*

* void Update()*
* {*
* if(Input.GetMouseButtonDown(1))*
* {*
* if(index > last)*
* index = first;*
* Camera.SetupCurrent(cams[index]);*
* for(int i = 0; i < cams.Length; i++)*
* {*
_ if(cams == Camera.current)
cams*.enabled = true;
else*

cams*.enabled = false;
}
index++;
print (Camera.current.gameObject.name + " Index = " + index);
}
}
}*_

Use an integer value to remember the current enabled camera. Then use a switch-case to determine what cameras are enabled/disabled based on that integer :

var camera1 : Camera;
var camera2 : Camera;
var camera3 : Camera;

private var currCam : int = 0;

function Start(){
	camera1.enabled=true;
	camera2.enabled=false;
	camera3.enabled=false;
	currCam = 0;
}

function Update(){
	if(Input.GetKeyDown("1")){
		currCam = ( currCam + 1 ) % 3;
		switch( currCam )
		{
			case 0 :
				camera1.enabled=true;
				camera2.enabled=false;
				camera3.enabled=false;
			break;
			
			case 1 :
				camera1.enabled=false;
				camera2.enabled=true;
				camera3.enabled=false;
			break;
			
			case 2 :
				camera1.enabled=false;
				camera2.enabled=false;
				camera3.enabled=true;
			break;
		}
	}
}

Well, making an array or having a counter would be much easier.

For example you can have a counter, and increase it by every single RMB.

if( Input.GetMouseButtonDown(1) ) {
    if( counter < 3) counter++;
    else counter = 0;
}

if( counter == 0){
    camera1.enabled = true;
    camera2.enabled = false;
    camera3.enabled = false;
} else if ( counter == 1) {
    camera1.enabled = false;
    camera2.enabled = true;
    camera3.enabled = false;
} else if ( counter == 2){
    camera1.enabled = false;
    camera2.enabled = false;
    camera3.enabled = true;
}

that would be one solution. Or you could have an array of cameras and activate only current one in the array, works same.