Created
August 2, 2022 22:39
-
-
Save Mylab6/57ee7110e4f1264fe107d36bd68ed805 to your computer and use it in GitHub Desktop.
Switch Cinemachine cameras during gameplay.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
using Cinemachine; | |
public class CamControl : MonoBehaviour | |
{ | |
public List<CinemachineVirtualCamera> virtualCameras; | |
private void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.Keypad1) || Input.GetKeyDown(KeyCode.Alpha1)) | |
{ | |
SwitchCam(0); | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.Alpha2)) | |
{ | |
SwitchCam(1); | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad3) || Input.GetKeyDown(KeyCode.Alpha3)) | |
{ | |
SwitchCam(2); | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.Alpha4)) | |
{ | |
SwitchCam(3); | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad5) || Input.GetKeyDown(KeyCode.Alpha5)) | |
{ | |
SwitchCam(4); | |
} | |
// Input.GetButton() | |
} | |
void SwitchCam(int nextCam) | |
{ | |
for (int i = 0; i < virtualCameras.Count; i++) | |
{ | |
if (nextCam == i) | |
{ | |
virtualCameras[i].gameObject.SetActive(true); | |
} | |
} | |
for (int i = 0; i < virtualCameras.Count; i++) | |
{ | |
if (nextCam != i) | |
{ | |
virtualCameras[i].gameObject.SetActive(false); | |
} | |
} | |
} | |
public void Awake() | |
{ | |
virtualCameras = FindObjectsOfType<Cinemachine.CinemachineVirtualCamera>().ToList(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment