Skip to content

Instantly share code, notes, and snippets.

@8ctopotamus
Created September 11, 2017 21:12
Show Gist options
  • Save 8ctopotamus/f9015d6830f8c2d08ab989259160760f to your computer and use it in GitHub Desktop.
Save 8ctopotamus/f9015d6830f8c2d08ab989259160760f to your computer and use it in GitHub Desktop.
Cycling through Cameras Unity3D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour {
public GameObject[] gameCameras;
private int gameCameraIndex = 0;
void Start () {
FocusOnCamera (gameCameraIndex);
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
ChangeCamera (1);
}
if (Input.GetMouseButtonDown (1)) {
ChangeCamera (-1);
}
}
void FocusOnCamera (int index) {
for (int i = 0; i < gameCameras.Length; i++) {
gameCameras [i].SetActive (i == index);
}
}
void ChangeCamera (int direction) {
gameCameraIndex += direction;
if (gameCameraIndex >= gameCameras.Length) {
gameCameraIndex = 0;
}
if (gameCameraIndex < 0) {
gameCameraIndex = gameCameras.Length - 1;
}
FocusOnCamera (gameCameraIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment