Using The Same Input Key To Switch Between Two Camera Views

I am fairly new to scripting and I want to be able to switch between two camera views using the same input key, in this case “1”. At the moment the script I created does’t work and was wondering if anybody could help me in making the script function as I intend it to.

The Script I currently have is:

var cameraFirstPerson : Camera;
var cameraThirdPerson : Camera;

function Start()
{
    cameraFirstPerson.enabled = true;
   	cameraThirdPerson.enabled = false;
}

function Update () 
{
    if (cameraThirdPerson == true || Input.GetKeyDown("1"))
    {
	cameraFirstPerson.enabled = true;
	cameraThirdPerson.enabled = false;
    }


    if (cameraFirstPerson == true || Input.GetKeyDown("1"))
    {
	cameraFirstPerson.enabled = false;
	cameraThirdPerson.enabled = true;
    }
}

You’re on the right path.

function Update(){
    if(Input.GetKeyDown("1")){
        cameraFirstPerson.gameObject.active = !cameraFirstPerson.gameObject.active;
        cameraThirdPerson.gameObject.active = !cameraFirstPerson.gameObject.active;
    }
}
  • Because of the “or” ||, both of your if are executed when you press 1.
  • By using the operator !, I toggle the boolean values (true == !false)
  • I use gameObject.active instead of enable. That disable all the components and it’s important because there is an AudioListener on your camera, and as Highlander, there can be only one.