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

avatar image

Sam Bigos
176 16 15 21

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

4 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

avatar image

duck ♦♦
49.5k 142 350 539

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, 2014 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, 2014 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

avatar image

Zoraph
16 3 6 7

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

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, 2014 at 08:18 AM exorakhilas
(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, 2014 at 06:22 PM

avatar image

wem1994
1

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

If you want to do this for many cameras, just use an array. Add an empty component in the inspector and attach this code. Then drag all cameras that you want to be able to cycle through to the array element "cameras"

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     public Camera[] cameras;
     private int currentCameraIndex;
     
     // Use this for initialization
     void Start () {
         currentCameraIndex = 0;
         
         //Turn all cameras off, except the first default one
         for (int i=1; i<cameras.Length; i++) 
         {
             cameras[i].gameObject.SetActive(false);
         }
         
         //If any cameras were added to the controller, enable the first one
         if (cameras.Length>0)
         {
             cameras [0].gameObject.SetActive (true);
             Debug.Log ("Camera with name: " + cameras [0].camera.name + ", is now enabled");
         }
     }
     
     // Update is called once per frame
     void Update () {
         //If the c button is pressed, switch to the next camera
         //Set the camera at the current index to inactive, and set the next one in the array to active
         //When we reach the end of the camera array, move back to the beginning or the array.
         if (Input.GetKeyDown(KeyCode.C))
         {
             currentCameraIndex ++;
             Debug.Log ("C button has been pressed. Switching to the next camera");
             if (currentCameraIndex < cameras.Length)
             {
                 cameras[currentCameraIndex-1].gameObject.SetActive(false);
                 cameras[currentCameraIndex].gameObject.SetActive(true);
                 Debug.Log ("Camera with name: " + cameras [currentCameraIndex].camera.name + ", is now enabled");
             }
             else
             {
                 cameras[currentCameraIndex-1].gameObject.SetActive(false);
                 currentCameraIndex = 0;
                 cameras[currentCameraIndex].gameObject.SetActive(true);
                 Debug.Log ("Camera with name: " + cameras [currentCameraIndex].camera.name + ", is now enabled");
             }
         }
     }
 }

more ▼

answered Feb 17 at 02:04 PM

avatar image

TheArtist
0 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:

x5399

asked: Apr 27, 2010 at 04:16 PM

Seen: 27664 times

Last Updated: Feb 17 at 02:11 PM