- Home /
Two cameras and switching
How do I set up a scene with two cameras that I will switch between and how do I reference them?
GameObject -> Create Other -> Camera -> then name the camera something like camera2.
Do I tag it too? and what do I tag it as? and how do I access it?
I've seen the following for accessing the main camera
Camera.main.enabled = false
If I create a new camera an name it camera2 can I just:
Camera.camera2.enabled = true
Thanks for any help!
I found this: http://www.unifycommunity.com/wiki/index.php?title=SwitchCamera
but I'm not that clear on how Unity knows which is camera1 and which is camera2.
Ok, in the inspector you can choose which GO camera goes with which var in the script that controls the cameras.
you can answer your question yourself and even choose it as the best answer. you just can not vote for it.
Answer by Ashkan_gc · Jun 30, 2010 at 06:42 PM
first of all i should say enabling/disabling any component is possible by the enabled property. if you want to make objects (GameObjects) enabled/disabled use their active property instead. for referencing any object you can do different things:
- use public variables and set them up in inspector
- use GameObject.Find or Object.FindObjectsOfType. there are many different methods in these two classes that they do what you want in different ways(using tags, components or names).
- set the variables of the manager object inside the objects that you want to reference. for example you can define a static list and tell each camera to add itself to the list in it's Awake function.
Answer by user-11096 (google) · Apr 21, 2011 at 02:10 AM
Well here is something that is really simple to do, to switch between cameras, this can also be used for aiming. Here it is, i can explain how it works at end of the code.
var camera1 : Camera;
var camera2 : Camera; private var startCamera : int = 1;
function Start () { camera1.enabled = true; camera2.enabled = false; startCamera = 1; }
function Update () { if(Input.GetKeyDown("f") && (startCamera == 1)) { startCamera = 2; camera1.enabled = false; camera2.enabled = true; } else if (Input.GetKeyDown("c") && (startCamera == 2)) { startCamera = 1; camera1.enabled = true; camera2.enabled = false; } }
ok the var camera1 = Camera; variable forces the slot to be a camera object the if and else if statements were checking if the key was pressed (you can change the key to anything you want), the && (startCamera == 2) (or startCamera ==1) makes the startCamera the second Camera. So thats why there is the camera_.enabled = true; (or) = false; there. You can keep this repettion when you play your game without needing to copy the gunction over and over again.
Your answer
Follow this Question
Related Questions
Character movement when I switch cameras. 0 Answers
Switching cameras (JavaScript) 0 Answers
How to make camera position relative to a specific target. 1 Answer
Add multiple cameras problems 1 Answer