Switching between 2 types of cameras

Hello everyone,

I’m currently making a 2D plateformer “Mario-style” where my character is moving in a labyrinth. In my test level, I set 8 cameras along the level, and a more distant one which allow the player to see the entire level.

I scripted my 8 cameras this way: when the player is between two values on the X and Y axis, it enables the concerning camera. My goal was to have a camera gesture like in Abe’s Odyssey or Abe’s Exodus (for people who played them). But I wanted also a distant one to have a view of the entire labyrinth and don’t get lost.

Here is a scheme of the placement of my 9 cameras (I didn’t draw the level, the picture would have been overloaded):

And here is the code I made for the automatic switch between the first 8 (the script is attached to the player’s character):

var camera1 : Camera; 
var camera2 : Camera; 
var camera3 : Camera;
var camera4 : Camera;
var camera5 : Camera;
var camera6 : Camera;
var camera7 : Camera;
var camera8 : Camera;
var cameraLarge : Camera;
 
function Start ()
{ 
   camera1.enabled = true; 
   camera2.enabled = false; 
   camera3.enabled = false;
   camera4.enabled = false;
   camera5.enabled = false;
   camera6.enabled = false;
   camera7.enabled = false;
   camera8.enabled = false;
   cameraLarge.enabled = false;
   startCamera = 1;
} 
 
function Update () 
{ 

	if (this.transform.position.x > 750  this.transform.position.y > 67.5)
	{
		startCamera = 1;
		camera1.enabled = true; 
		camera2.enabled = false; 
		camera3.enabled = false;
		camera4.enabled = false;
		camera5.enabled = false;
		camera6.enabled = false;
		camera7.enabled = false;
		camera8.enabled = false;
   	}
   	
   	else if (this.transform.position.x < 750  this.transform.position.x >= 500  this.transform.position.y > 67.5)
   	{ 
	  	startCamera = 2;
		camera1.enabled = false; 
		camera2.enabled = true; 
		camera3.enabled = false;
		camera4.enabled = false;
		camera5.enabled = false;
		camera6.enabled = false;
		camera7.enabled = false;
		camera8.enabled = false;
   	}
   	
   	else if (this.transform.position.x < 500  this.transform.position.x >= 250  this.transform.position.y > 67.5)
   	{ 
	  	startCamera = 3;
		camera1.enabled = false; 
		camera2.enabled = false; 
		camera3.enabled = true;
		camera4.enabled = false;
		camera5.enabled = false;
		camera6.enabled = false;
		camera7.enabled = false;
		camera8.enabled = false;
   	}
   	
   	//... and this goes on for the 8 cameras...
   	
}

But I am not able to manage the distant one, called “cameraLarge”. What I want is when the player press the “c” button, all the 8 cameras are disabled and only the distant one is enabled. and when he presses again the “c” button, normal view between the 8 is set back.
I tried something like:

if (Input.GetKeyDown ("c"))
		cameralarge.enabled = true;
		camera1.enabled = false; 
		camera2.enabled = false; 
		camera3.enabled = false;
		camera4.enabled = false;
		camera5.enabled = false;
		camera6.enabled = false;
		camera7.enabled = false;
		camera8.enabled = false;

but nothing works…

Thank you for your help if anyone have an idea.

To be honest, but I do not see a reason to create 9 different cameras at all. I would rather simply move the cam around…

I agree with xWebber. Move the cam around. You could have little empty GameObjects laying around the world, and then move the cam to their positions.

I know that you are looking for a solution, but here’s a suggestion for improvement, so that you may actually finish your product without the code getting out of hand: Look into loops and arrays.

I hope your project goes well.

I agree my current script is long and not optimized. I can see it when I load my sceen each time I want to try it: the 9 cams make the loading be quite long. I’m gonna try tomorrow to associate only 1 cam to several game object and make it switch between them. Thank you guys.
BTW, does anyone have an idea concerning the way to enable/desable my large camera by pressing the «c» button?