- Home /
Camera switching trouble
I'm having trouble switching between two cameras using the code below. If I move 1 camera, the other one moves too. How do I stop this happening?
var cam1 : Camera; var cam2 : Camera;
function Update () {
if(Input.GetKeyDown("1")){ cam1.enabled = true; cam2.enabled = false; }
if(Input.GetKeyDown("2")){
cam1.enabled = false;
cam2.enabled = true;
}
}
Answer by GeirGrusom_Legacy1 · Nov 12, 2010 at 08:47 PM
The cameras can not be part of the same transform.
Answer by SrBilyon · Nov 12, 2010 at 09:40 PM
var camera1 : Camera; //Place first camera in the inspector under this variable var camera2 : Camera; //Place second camera in the inspector under this variable
function Start () { camera1.enabled = true; camera2.enabled = false; }
function Update () { if (Input.GetKeyDown ("2")){ camera1.enabled = false; camera2.enabled = true; } if (Input.GetKeyDown ("1")){ camera1.enabled = true; camera2.enabled = false; }
That should fix the problem. BTW, this is from the Wiki: http://www.unifycommunity.com/wiki/index.php?title=SwitchCamera
Nop, this doesn't work. I can switch between the camera views but when I use the W-A-S-D keys or arrow keys to move around, both the cameras move.
Found the problem. I had to use camera.active ins$$anonymous$$d of camera.enabled and then dragged the whole camera GameObject into the relevant inspector item.
Implying that you cameras are attached to the player object?
Your answer
Follow this Question
Related Questions
Switching cameras? 1 Answer
camera switch not working 3 Answers
Single Camera Lerping between three characters? (code provided) 0 Answers
Flying particles and switching between cameras 0 Answers
UI Buttons switch cameras 3 Answers