For a Video Game Design class project, I have been working on a simple solar system. It has all of the planets rotating around the the sun, etc. I wanted to create a GUI element where you could click on a button with the name of the planet (ex: button says “Earth”), and the camera would switch to follow that planet’s orbit. I read up on the scripting reference, and I came up with something that worked after much forum browsing/trial and error (I am pretty new to scripting). I was wondering if someone could show me a more efficient way to do this. First, I’ll show the way I came up with.

BasicalIy, my method was to make a camera for each planet, and attach this script to each:

#pragma strict
var target : Transform;


function Update () {
	transform.LookAt(target);

}

That one makes the camera look at its designated planet. The other script was for the GUI:

#pragma strict

var mainview : Camera;
var marsview : Camera;
var earthview : Camera;
var mercuryview : Camera;
var jupiterview : Camera;
var plutoview : Camera;
//states variables of the cameras

function Start () {
	//deactivates all cameras but the main camera
	mainview.camera.active = true;
	marsview.camera.active = false;
	earthview.camera.active = false;
	mercuryview.camera.active = false;
	jupiterview.camera.active = false;
	
}


function OnGUI () {
	//creates GUI
	GUI.Box (Rect (0, 10, 200, 90), "Camera View Switch");
	if (GUI.Button (Rect (20, 40, 80, 20), "Earth")) { //first button
		mainview.camera.active = false;
		marsview.camera.active = false;
		mercuryview.camera.active = false;
		jupiterview.camera.active = false;
		earthview.camera.active = true;
		//activates earthview camera, deactivates all others
	}	
	if (GUI.Button (Rect (20, 70, 80, 20), "Mars")) {
   		mainview.camera.active = false;
		earthview.camera.active = false;
		mercuryview.camera.active = false;
		jupiterview.camera.active = false;
		marsview.camera.active = true;
	}
	if (GUI.Button (Rect (20, 100, 80, 20), "Sun")) {
		earthview.camera.active = false;
		marsview.camera.active = false;
		mercuryview.camera.active = false;
		jupiterview.camera.active = false;
		mainview.camera.active = true;
	}
	if (GUI.Button (Rect (20, 130, 80, 20), "Mercury")) {
		earthview.camera.active = false;
		marsview.camera.active = false;
		mainview.camera.active = false;
		jupiterview.camera.active = false;
		mercuryview.camera.active = true;
	}
	if (GUI.Button (Rect (20, 160, 80, 20), "Jupiter")) {
		earthview.camera.active = false;
		marsview.camera.active = false;
		mainview.camera.active = false;
		mercuryview.camera.active = false;
		jupiterview.camera.active = true;
	}
	if (GUI.Button (Rect (20, 190, 80, 20), "Pluto")) {
		earthview.camera.active = false;
		marsview.camera.active = false;
		mainview.camera.active = false;
		mercuryview.camera.active = false;
		jupiterview.camera.active = false;
		plutoview.camera.active = true;
	}

}

function Update () {

}

I didn’t include all of the planets for the sake of example, but you get the idea. Being pretty new to this, I was glad I could find a solution that worked. Now I am looking for community input. If it isn’t too much trouble, could someone show me a better, more efficient/effective way to accomplish this?

You could just use an array for your cameras, and then loop through the array, given an index activate that camera, and disable all others in a loop :

#pragma strict

// Drop cameras in Inspector **IN ORDER**
// eg Main; Sun; Mercury; Venus; Earth; etc etc

var cameraView : Camera[];


function OnGUI() 
{
	if (GUI.Button (Rect (20, 40, 80, 20), "Earth"))
	{
		SetCamera( 4 ); // 5th item in list, 4th counting from zero
	}
	if (GUI.Button (Rect (20, 70, 80, 20), "Mars"))
	{
		SetCamera( 5 ); // 6th item in list, 5th counting from zero
	}
	if (GUI.Button (Rect (20, 100, 80, 20), "Sun"))
	{
		SetCamera( 1 ); // 2nd item in list, 1st counting from zero
	}
}


function SetCamera( index : int ) 
{
	for ( var i : int = 0; i < cameraView.Length; i ++ )
	{
		cameraView*.enabled = false;*
  • }*

  • cameraView[index].enabled = true;*
    }

// this could also be written like :
/*
function SetCamera( index : int )
{

  • for ( var i : int = 0; i < cameraView.Length; i ++ )*
  • {*
  •  if ( i == index )*
    
  •  {*
    

_ cameraView*.enabled = true;_
_
}_
_
else*_
* {*
_ cameraView*.enabled = false;
}
}
}
/_