TheLetterM

Warp: Camera Controller

May 13th, 2023
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.47 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraController : MonoBehaviour
  6. {
  7.  
  8.     public GameObject player1, player2; // Stores references to the two players
  9.  
  10.     public GameObject focusPoint; // The camera will follow this gameObject, not either of the players under normal conditions
  11.     public GameObject target; // Allows camera to toggle between following the focus to following a player by switching this
  12.  
  13.     [Header("Variables")]
  14.     public float calculatedSize;
  15.     public float minimumSize;
  16.     public float zoomSize; // This value is used instead of minimumSize when a player is killed to create a kill camera.
  17.     public float distance;
  18.  
  19.     public bool isZooming;
  20.  
  21.     Camera theCamera;
  22.     // Start is called before the first frame update
  23.     void Start()
  24.     {
  25.         theCamera = GetComponent<Camera>();
  26.         SoundManager.instance.Play("LevelMusic"); // Start playing the level BGM and stop playing the menu BGM
  27.         SoundManager.instance.StopPlaying("MenuMusic");
  28.         focusPoint = GameObject.Find("FocusPoint");
  29.         target = focusPoint;
  30.  
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.  
  37.         Vector3 v = (player1.transform.position + player2.transform.position) / 2f; // Calculates the midpoint between the two players
  38.  
  39.         // Calculate cameraSize to use
  40.         Vector3 p1 = new Vector3(player1.transform.position.x, 0, player1.transform.position.z);
  41.         Vector3 p2 = new Vector3(player2.transform.position.x, 0, player2.transform.position.z);
  42.  
  43.         distance = Vector3.Distance(p1, p2); // Calculates distance between the players
  44.         calculatedSize = distance * 0.8f;
  45.  
  46.         focusPoint.transform.position = v; // Sets the position of the focusPoint to this position
  47.  
  48.         if (isZooming) return; // If we are zooming in during the deathCam, do not set position of the camera in the transform
  49.  
  50.         float s = Mathf.Clamp(calculatedSize, minimumSize, calculatedSize);
  51.         theCamera.orthographicSize = Mathf.Lerp(theCamera.orthographicSize, s, Time.deltaTime * 4f);
  52.         // If calculatedSize is greater than minimumSize, use the original value. If calculatedSize is smaller than minimumSize, use minimumSize instead.
  53.  
  54.         Vector3 d = new Vector3(
  55.             focusPoint.transform.position.x + (-17.6f),
  56.             21f, //focusPoint.transform.position.y + 21f, y-value is constant so camera doesnt follow player or change size down when they die.
  57.             focusPoint.transform.position.z + (-16.5f)); // Move camera to
  58.  
  59.         // Smoothly lerp to focusPoint's position.
  60.         transform.position = Vector3.Lerp(transform.position, d, Time.deltaTime * 3f);
  61.  
  62.     }
  63.  
  64.     public IEnumerator DeathCam(GameObject zoomTarget)
  65.     {
  66.         if (isZooming) yield break;
  67.         float t = 0f;
  68.         float duration = 3f; //float duration = 300
  69.  
  70.         target = zoomTarget;
  71.         isZooming = true;
  72.  
  73.         //bool isZoomingOut = false;
  74.         Vector3 destination = new Vector3(
  75.             zoomTarget.transform.position.x + (-17.6f),
  76.             21f, //zoomTarget.transform.position.y + 21f,
  77.             zoomTarget.transform.position.z + (-16.5f)); // The position to lerp towards, this is a point above the dead player
  78.  
  79.  
  80.         while (t < duration)
  81.         {
  82.             //Debug.Log("Lerping");
  83.             //if (isZoomingOut == false)
  84.             transform.position = Vector3.Lerp(transform.position, destination, t / duration / 2); // Moves the camera over to the dead player
  85.             theCamera.orthographicSize = Mathf.Lerp(theCamera.orthographicSize, zoomSize, t / duration / 2); // Zoom in the camera on the player's corpse
  86.  
  87.             t += Time.deltaTime;
  88.             yield return new WaitForEndOfFrame();
  89.         }
  90.  
  91.         //yield return new WaitUntil(() => transform.position == destination); // Once the camera has reached the destination, zoom back out
  92.         yield return new WaitForEndOfFrame();
  93.  
  94.         target = focusPoint; // Set the target back to the focusPoint of the camera, which is midpoint between two players
  95.  
  96.         destination = new Vector3(
  97.             target.transform.position.x + (-17.6f),
  98.             21f,
  99.             target.transform.position.z + (-16.5f)); // Change destination to be based off focusPoint position
  100.  
  101.         t = 0f; // Reset t
  102.  
  103.         //Return camera to target position and regular size
  104.         while (t < duration)
  105.         {
  106.             transform.position = Vector3.Lerp(transform.position, destination, (t / duration) / 2);
  107.  
  108.             float s = Mathf.Clamp(calculatedSize, minimumSize, calculatedSize); // If the calculated size is smaller than minimum size, use minimum size
  109.             theCamera.orthographicSize = Mathf.Lerp(theCamera.orthographicSize, s, (t / duration) / 2);
  110.  
  111.             t += Time.deltaTime;
  112.             yield return new WaitForEndOfFrame();
  113.         }
  114.  
  115.         // Revive and re-enable the dead players
  116.         foreach (GameObject player in GameManager.inst.deadPlayers)
  117.         {
  118.             player.GetComponent<PlayerCube>().isDead = false;
  119.             player.GetComponent<PlayerCube>().playerMesh.SetActive(true);
  120.         }
  121.  
  122.         // Destroy the two checkpoints
  123.         Destroy(GameManager.inst.playerOneSpawn);
  124.         Destroy(GameManager.inst.playerTwoSpawn);
  125.         GameManager.inst.deadPlayers.Clear();
  126.         isZooming = false;
  127.  
  128.         GameManager.inst.StartCoroutine(GameManager.inst.SpawnLevel());
  129.  
  130.         yield break;
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment