x


Changing between cameras

I want to have two different cameras in my game, one top down and one 3rd person, and I want them to be changeable when you press a key, or change an option in the GUI. Is it possible to select a different main camera or would I have to move the main camera?

more ▼

asked Apr 27, 2010 at 04:16 PM

Sam Bigos gravatar image

Sam Bigos
151 16 15 20

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You can do either, although it's probably simpler to switch between cameras.

To change the main camera, you can set the "active" properties of the camera gameobjects, or you can set the "enabled" properties of the camera components on the gameobjects. It's often preferable to use the enabled property, which is shown below:

Camera.main refers to the first enabled camera tagged with "MainCamera", so you can tag both cameras with this tag, and use a simple script to toggle between them like this:

var cam1 : Camera; var cam2 : Camera;

function Start() { cam1.enabled = true; cam2.enabled = false; }

function Update() {

 if (Input.GetKeyDown(KeyCode.C)) {
     cam1.enabled = !cam1.enabled;
     cam2.enabled = !cam2.enabled;
 }

}

Place this script on a gameobject in your scene (for example, your player, or a new empty gameobject), and drag references to both your cameras into the "cam1" and "cam2" variables respectively.

The static variable "Camera.main" will always refer to the currently active camera out of these two.

more ▼

answered Apr 27, 2010 at 04:25 PM

duck gravatar image

duck ♦♦
47.8k 131 233 472

Worked great thanks!

Apr 27, 2010 at 06:15 PM Sam Bigos

how could i edit this for use with a button? it works for key down but when i try to create a button to do the same function it doesn't appear?

May 28, 2013 at 09:58 AM gazza529

can we write the script in c#? how to write it?

Jul 03 at 11:45 AM fadihasrouni

Is is possible to have two cameras running (you can access their properties etc) but you can only see through the eyes of one of the cameras?

Oct 13 at 08:04 AM Ogen
(comments are locked)
10|3000 characters needed characters left

Here's how I did it with three cameras:

 var cam1: Camera;
 var cam2: Camera;
 var cam3: Camera;
 
 function Start() {
     cam1.enabled = true;
     cam2.enabled = false;
     cam3.enabled = false;
 }
 
 function Update() {
 
     if (Input.GetKeyDown(KeyCode.V) && (cam1.enabled == true || cam3.enabled == true)) {
     cam1.enabled = false;
     cam2.enabled = true;
     cam3.enabled = false;
     }
     else if (Input.GetKeyDown(KeyCode.B) && (cam2.enabled == true || cam1.enabled == true)) {
     cam1.enabled = false;
     cam2.enabled = false;
     cam3.enabled = true;
     }
     else if (Input.GetKeyDown(KeyCode.C) && (cam2.enabled == true || cam3.enabled == true)) {
     cam1.enabled = true;
     cam2.enabled = false;
     cam3.enabled = false;
     }
 }
more ▼

answered Aug 25, 2012 at 10:26 PM

Zoraph gravatar image

Zoraph
16 3 6 7

Both Right..! Excellent!! XD

Jan 18, 2013 at 03:49 PM Chuckler

Matthew0123, your solution is correct but a specific button is needed to switch between camera, the former code is alright.

Sep 27 at 08:18 AM exorakhilas

Why would you use 3 different inputs? This is a better way to do it.

 if (Input.GetKeyDown(KeyCode.V) && (cam1.enabled == true)) {
 cam1.enabled = false;
 cam2.enabled = true;
 cam3.enabled = false;
 }
 else if (Input.GetKeyDown(KeyCode.V) && (cam2.enabled == true)) {
 cam1.enabled = false;
 cam2.enabled = false;
 cam3.enabled = true;
 }
 else if (Input.GetKeyDown(KeyCode.V) && (cam2.enabled == true)) {
 cam1.enabled = true;
 cam2.enabled = false;
 cam3.enabled = false;
 }

This way you use only one input key and you will switch your views of your camera.

Jan 06, 2013 at 10:07 AM Matthew0123
(comments are locked)
10|3000 characters needed characters left

how about when i have 3 or more cameras but i want them to switch using gui buttons how can i do that?

more ▼

answered Jan 25 at 06:22 PM

wem1994 gravatar image

wem1994
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4933

asked: Apr 27, 2010 at 04:16 PM

Seen: 20835 times

Last Updated: Oct 13 at 08:04 AM