Switch between 5 cameras in the game by clicking.
I've been trying to figure this out for a while now and have tried 10 different scripts and changing the position of the camera instead of a different camera. I'm trying to cycle through 5 cameras by clicking rmb and it only works for the first camera and not the rest. Please help.
using UnityEngine; using System.Collections;
public class CameraCont : MonoBehaviour {
public Camera leftCam;
public Camera topCam;
public Camera rightCam;
public Camera frontCam;
public Camera backCam;
void Start () {
topCam.enabled = true;
leftCam.enabled = false;
rightCam.enabled = false;
frontCam.enabled = false;
backCam.enabled = false;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
topCam.enabled = false;
leftCam.enabled = true;
rightCam.enabled = false;
frontCam.enabled = false;
backCam.enabled = false;
} else if (Input.GetButtonDown ("Fire1")) {
topCam.enabled = false;
leftCam.enabled = false;
rightCam.enabled = true;
frontCam.enabled = false;
backCam.enabled = false;
} else if (Input.GetButtonDown ("Fire1")) {
topCam.enabled = false;
leftCam.enabled = false;
rightCam.enabled = false;
frontCam.enabled = true;
backCam.enabled = false;
}
else if (Input.GetButtonDown ("Fire1")) {
topCam.enabled = false;
leftCam.enabled = false;
rightCam.enabled = false;
frontCam.enabled = false;
backCam.enabled = true;
}
}
}
The problem with your code is the way you check for Input. You check if the button is pressed; if yes, topcam is enabled and the rest is disabled. If not, then you again ask if the button is pressed, and so on. This doesn't make sense since it will naturally never be true; If a button is not pressed, it can't be pressed at the same time.
Here's a way that makes it more manageable with multiple cameras.
public Camera[] cameras;
public int activeIndex = 0;
void Update() {
if(Input.GetButtonDown ("Fire1")) {
activeIndex ++;
if(activeIndex >= cameras.Length) {
activeIndex = 0;
}
for(int i = 0; i < cameras.Length; i++) {
Camera camera = cameras[i];
if(i == activeIndex) {
camera.enabled = true;
}
else {
camera.enabled = false;
}
}
}
}
Your answer
Follow this Question
Related Questions
How can I get all cameras enabled true false states ? 0 Answers
Checking if canvas world space is visible in CenterEyeAnchor? 0 Answers
c# - error CS0103: The name `hit' does not exist in the current context (cardboard switching) 1 Answer
Why are my keys being registered multiple times? 1 Answer
Can someone help me with this camera orbiting script? 0 Answers