- Home /
Multiple Camera Switching
Ok I have a script here that works perfectly but I want to use one button to switch cameras (Camera1, Camera2 ...Camera5). I am using a ps3 controller and I want to use my Triangle button (joystick button 3) using the Input.GetAxis method which is called "Camera". The mirrors are minimaps attached to Camera1 and Camera2 Can anyone help? Here is the original script that works and my attempt to use Input.GetAxis script:
Original script:
var mirrorOne : GameObject;
var mirrorTwo : GameObject;
function Start ()
{
camSwap(1);
mirrorOne.GetComponent("Camera").enabled = true;
mirrorTwo.GetComponent("Camera").enabled = false;
}
function Update ()
{
if(Input.GetKey("1"))
{
camSwap(1);
mirrorOne.GetComponent("Camera").enabled = true;
mirrorTwo.GetComponent("Camera").enabled = false;
}
if(Input.GetKey("2"))
{
camSwap(2);
mirrorOne.GetComponent("Camera").enabled = false;
mirrorTwo.GetComponent("Camera").enabled = true;
}
if(Input.GetKey("3"))
{
camSwap(3);
mirrorOne.GetComponent("Camera").enabled = false;
mirrorTwo.GetComponent("Camera").enabled = false;
}
if(Input.GetKey("4"))
{
camSwap(4);
mirrorOne.GetComponent("Camera").enabled = false;
mirrorTwo.GetComponent("Camera").enabled = false;
}
if(Input.GetKey("5"))
{
camSwap(5);
mirrorOne.GetComponent("Camera").enabled = false;
mirrorTwo.GetComponent("Camera").enabled = false;
}
}
function camSwap(currentCam : int)
{
var cameras = GameObject.FindGameObjectsWithTag("MainCamera");
for (var cams : GameObject in cameras)
{
cams.GetComponent(Camera).enabled = false;
cams.GetComponent(AudioListener).enabled = false;
}
var oneToUse : String = "Camera"+currentCam;
gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
gameObject.Find(oneToUse).GetComponent(AudioListener).enabled = true;
}
My Attempt:
var mirrorOne : GameObject;
var mirrorTwo : GameObject;
var cam : int;
function Start ()
{
cam = 1;
camSwap(1);
mirrorOne.GetComponent("Camera").enabled = true;
mirrorTwo.GetComponent("Camera").enabled = false;
}
function Update ()
{
if(Input.GetAxis("Camera") && cam ==1)
{
cam = 2;
camSwap(2);
mirrorOne.GetComponent("Camera").enabled = true;
mirrorTwo.GetComponent("Camera").enabled = false;
}
if(Input.GetAxis("Camera") && cam ==2)
{
cam = 3;
camSwap(3);
mirrorOne.GetComponent("Camera").enabled = false;
mirrorTwo.GetComponent("Camera").enabled = true;
}
if(Input.GetAxis("Camera") && cam ==3)
{
cam = 4;
camSwap(4);
mirrorOne.GetComponent("Camera").enabled = false;
mirrorTwo.GetComponent("Camera").enabled = false;
}
if(Input.GetAxis("Camera") && cam ==4)
{
cam = 5;
camSwap(5);
mirrorOne.GetComponent("Camera").enabled = false;
mirrorTwo.GetComponent("Camera").enabled = false;
}
if(Input.GetAxis("Camera") && cam ==5)
{
cam = 1;
camSwap(1);
mirrorOne.GetComponent("Camera").enabled = true;
mirrorTwo.GetComponent("Camera").enabled = false;
}
}
function camSwap(currentCam : int)
{
var cameras = GameObject.FindGameObjectsWithTag("MainCamera");
for (var cams : GameObject in cameras)
{
cams.GetComponent(Camera).enabled = false;
cams.GetComponent(AudioListener).enabled = false;
}
var oneToUse : String = "Camera"+currentCam;
gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
gameObject.Find(oneToUse).GetComponent(AudioListener).enabled = true;
}
O$$anonymous$$ now this is pissing me off!!!
1) The cameras only cycle if there is 1 or no mirrors dragged into the inspector but I get an error saying that I am missing the mirror variable/s, which makes perfect sense to me.
2) But If I add both mirrors, the mirrors don't activate/deactivate and the cameras cycle from 1 through 5 and back to 1, with camera1's view never changing but the console shows the rapid cycle (1,2,3,4,5,1).
3) The mirrors only work if all the cameras are manually disabled in the hierarchy but obviously I have no cameras or listeners.
Here is the script:
import System.Collections.Generic;
import System.Linq;
var mirrorOne : GameObject;
var mirrorTwo : GameObject;
var cam : int;
function Start ()
{
Debug.Log("Cam 1");
cam = 1;
camSwap(1);
mirrorOne.SetActive (true);
mirrorTwo.SetActive (false);
}
function Update ()
{
if(Input.GetButtonDown("Camera") && cam == 1)
{
Debug.Log("Cam 2");
cam = 2;
camSwap(2);
mirrorOne.SetActive (false);
mirrorTwo.SetActive (true);
}
if(Input.GetButtonDown("Camera") && cam == 2)
{
Debug.Log("Cam 3");
cam = 3;
camSwap(3);
mirrorOne.SetActive (false);
mirrorTwo.SetActive (false);
}
if(Input.GetButtonDown("Camera") && cam == 3)
{
Debug.Log("Cam 4");
cam = 4;
camSwap(4);
mirrorOne.SetActive (false);
mirrorTwo.SetActive (false);
}
if(Input.GetButtonDown("Camera") && cam == 4)
{
Debug.Log("Cam 5");
cam = 5;
camSwap(5);
mirrorOne.SetActive (false);
mirrorTwo.SetActive (false);
}
if(Input.GetButtonDown("Camera") && cam == 5)
{
Debug.Log("Cam 1");
cam = 1;
camSwap(1);
mirrorOne.SetActive (true);
mirrorTwo.SetActive (false);
}
}
function camSwap(currentCam : int)
{
var cameras = GameObject.FindGameObjectsWithTag("$$anonymous$$ainCamera");
for (var cams : GameObject in cameras)
{
cams.GetComponent(Camera).enabled = false;
cams.GetComponent(AudioListener).enabled = false;
}
var oneToUse : String = "Camera"+currentCam;
gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
gameObject.Find(oneToUse).GetComponent(AudioListener).enabled = true;
}
Answer by SwagGamez · Jan 05, 2016 at 08:21 AM
Ok I figured it out. After the first if-statement, in the Update function, the rest of the ifs need to be else-if-statements. Works perfect now. Thanx for your help though fellas.
Glad you've figured it out. $$anonymous$$ind if I use that script?
Your answer
Follow this Question
Related Questions
Camera Switching or Scene Changes? 3 Answers
cameras switch doesn't work 1 Answer
Switching Between Cameras JavaScript 0 Answers
switch character 1 Answer
Switching cameras (JavaScript) 0 Answers