Skip to content

Instantly share code, notes, and snippets.

@GT3000
Created January 31, 2023 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GT3000/3f2cf2e55ba8ecf950a2524affdd019b to your computer and use it in GitHub Desktop.
Save GT3000/3f2cf2e55ba8ecf950a2524affdd019b to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using Cinemachine;
public class SecuritySystem : MonoBehaviour
{
[SerializeField] protected CinemachineBrain brain;
[SerializeField] protected CinemachineVirtualCamera[] virtualCams;
[SerializeField] protected CinemachineVirtualCamera thirdPersonCamera;
[SerializeField] protected bool inZone = false;
[SerializeField] protected int currentCamera;
[SerializeField] protected int fov = 60;
protected int fovZoomLevel;
// Update is called once per frame
void Update()
{
//If the capsule is in the trigger zone
if (inZone)
{
//Cycle through cameras using the C keycode
if (Input.GetKeyDown(KeyCode.C))
{
currentCamera++;
if (currentCamera > virtualCams.Length - 1)
{
if (currentCamera > virtualCams.Length - 1)
{
virtualCams[currentCamera - 1].Priority = 10;
}
currentCamera = 0;
virtualCams[currentCamera].Priority = 11;
}
else
{
virtualCams[currentCamera].Priority = 11;
}
if (currentCamera > 0)
{
virtualCams[currentCamera - 1].Priority = 10;
}
}
//If the current camera is set to the FOV camera (which is the last camera in list of 3) zoom in when you press space
if (Input.GetKeyDown(KeyCode.Space) && currentCamera == 2)
{
if (fovZoomLevel < 2)
{
fovZoomLevel++;
fov -= 20;
}
else
{
fovZoomLevel = 0;
fov = 60;
}
brain.ActiveVirtualCamera.VirtualCameraGameObject.GetComponent<CinemachineVirtualCamera>().m_Lens.FieldOfView = fov;
}
}
}
//Switch from the third person camera on the capsule to the first camera in the list when you enter the zone
private void OnTriggerEnter(Collider other)
{
if (other.transform.CompareTag("Player") && !inZone)
{
inZone = true;
thirdPersonCamera.Priority = 10;
foreach (var camera in virtualCams)
{
if (camera != null)
{
camera.Priority = 10;
if (camera == virtualCams[0])
{
camera.Priority = 11;
}
}
}
}
}
//Switch from the current camera to the third person camera on teh capsule when you enter you exit the zone
private void OnTriggerExit(Collider other)
{
if (inZone)
{
inZone = false;
foreach (var camera in virtualCams)
{
if (camera != null)
{
if (camera == virtualCams[0])
{
camera.Priority = 10;
}
}
}
thirdPersonCamera.Priority = 11;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment