Switching Cameras

Is there a scritp that will switch cameras when a certain key is pressed?

There isn’t one on the wiki that I know of, so unless someone feels like posting some code, no.

But it isn’t too hard to do yourself. From script, just disable one camera and enable another, as far as I remember. I haven’t had the need to do it yet so I haven’t researched farther, but it isn’t very hard.

Check out the script ref for the Camera class, that should give you some ideas.

HTH,
-Jeremy

okay…thanks
heres what I have:

var camera1 : GameObject;
var camera2 : GameObject;

function Start () {
camera1.camera.enabled = true;
camera2.camera.enabled = false;
}

function Update () {
if (Input.GetKeyDown (“0”)){
camera1.camera.enabled = false;
camera2.camera.enabled = true;
}
if (Input.GetKeyDown (“9”)){
camera1.camera.enabled = true;
camera2.camera.enabled = false;
}
}

Glad it worked for you. :slight_smile:

Btw, you want to use the code tags when you paste code on the forums, makes it look nicer.

//like this.
[code]my code

[/code]

-Jeremy

Thanks for the tip :smile:

Np. People will yell at you and chase you with pitchforks if you just paste larger code samples.

Ok ok, “people” is just me and another guy. :wink:

You will usually get faster help this way too because we can read the code at a glance.

-Jeremy

do you know how some spaceship games have two cross hairs for targeting (my mind is drawing a blank on names) is there a way to do that in unity?

There is always a way to do most things in Unity. :wink:

“two crosshairs” is a bit vauge though. What exactly are you looking for?

-Jeremy

Here’s a script that will toggle cameras with the scroll wheel:

// Place this onto any gameobject in the scene.
var cameras : Camera[];
var currentCamera : int = 0;

function Update ()
{
	var axis : float = Input.GetAxis ("Mouse ScrollWheel");
	if (!axis)
		return;
		
	if (axis > 0.0)
		currentCamera = currentCamera == cameras.length - 1 ? 0 : currentCamera + 1;
	else
		if (axis < 0.0)
			currentCamera = currentCamera == 0 ? cameras.length - 1 : currentCamera - 1;
	UpdateCameras ();
}

function UpdateCameras ()
{
	for (var i = 0; i < cameras.length; i++)
		cameras [i].enabled = currentCamera == i ? true : false;
}

somthing like this…I hope you can understand it :?

Are you talking about the cross hair. If so, the script would be something like this:

var ship : Transform;

function Update ()
{
     transform.position = Camera.main.WorldToViewportPoint (ship.position);
}

Place this script onto the cross hair gui texture.

I attatched the script but it just stays in one place when the ship moves

is there also a way to that when you switch to a diffrent camera you can make an object transparent and a GUI texture apear?