Skip to content

Instantly share code, notes, and snippets.

@Mylab6
Created August 2, 2022 22:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mylab6/57ee7110e4f1264fe107d36bd68ed805 to your computer and use it in GitHub Desktop.
Save Mylab6/57ee7110e4f1264fe107d36bd68ed805 to your computer and use it in GitHub Desktop.
Switch Cinemachine cameras during gameplay.
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