Advertisement
Sweetening

Killcam Code By Taylor Christian Newsome

Jul 26th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. using System.Collections;
  2.  
  3. using UnityEngine;
  4.  
  5. using UnityEngine.UI;
  6.  
  7.  
  8.  
  9. public class KillCamManager : MonoBehaviour
  10.  
  11. {
  12.  
  13. [Header("Camera Settings")]
  14.  
  15. public Camera mainCamera;
  16.  
  17. public Camera killCam;
  18.  
  19. public float killCamDuration = 5.0f;
  20.  
  21. public float transitionDuration = 1.0f;
  22.  
  23. public float shakeIntensity = 0.5f;
  24.  
  25.  
  26.  
  27. [Header("UI Elements")]
  28.  
  29. public Canvas killCamCanvas;
  30.  
  31. public Text killCamText;
  32.  
  33. public Image fadeImage;
  34.  
  35. public GameObject killCamEffectPrefab;
  36.  
  37.  
  38.  
  39. private bool isKillCamActive = false;
  40.  
  41. private KillCamData currentKillCamData;
  42.  
  43. private GameObject killCamEffectInstance;
  44.  
  45. private Coroutine fadeCoroutine;
  46.  
  47.  
  48.  
  49. void Start()
  50.  
  51. {
  52.  
  53. if (!mainCamera || !killCam || !killCamCanvas || !fadeImage)
  54.  
  55. {
  56.  
  57. Debug.LogError("KillCamManager: Missing required components.");
  58.  
  59. enabled = false;
  60.  
  61. return;
  62.  
  63. }
  64.  
  65.  
  66.  
  67. killCamCanvas.enabled = false;
  68.  
  69. fadeImage.color = new Color(0, 0, 0, 0);
  70.  
  71. }
  72.  
  73.  
  74.  
  75. void Update()
  76.  
  77. {
  78.  
  79. if (isKillCamActive)
  80.  
  81. {
  82.  
  83. ApplyCameraShake();
  84.  
  85. }
  86.  
  87. }
  88.  
  89.  
  90.  
  91. public void TriggerKillCam(Vector3 killerPosition, Quaternion killerRotation, string killerName, string victimName)
  92.  
  93. {
  94.  
  95. if (isKillCamActive) return;
  96.  
  97.  
  98.  
  99. currentKillCamData = new KillCamData
  100.  
  101. {
  102.  
  103. killerPosition = killerPosition,
  104.  
  105. killerRotation = killerRotation,
  106.  
  107. killerName = killerName,
  108.  
  109. victimName = victimName
  110.  
  111. };
  112.  
  113.  
  114.  
  115. StartCoroutine(PlayKillCam());
  116.  
  117. }
  118.  
  119.  
  120.  
  121. private IEnumerator PlayKillCam()
  122.  
  123. {
  124.  
  125. isKillCamActive = true;
  126.  
  127.  
  128.  
  129. // Fade out main camera
  130.  
  131. if (fadeCoroutine != null)
  132.  
  133. {
  134.  
  135. StopCoroutine(fadeCoroutine);
  136.  
  137. }
  138.  
  139. fadeCoroutine = StartCoroutine(FadeScreen(true));
  140.  
  141.  
  142.  
  143. // Set up KillCam
  144.  
  145. SetUpKillCam();
  146.  
  147.  
  148.  
  149. // Start killcam effect
  150.  
  151. StartKillCamEffect();
  152.  
  153.  
  154.  
  155. // Transition to KillCam view
  156.  
  157. yield return StartCoroutine(CameraTransition(killCam.transform.position, killCam.transform.rotation));
  158.  
  159.  
  160.  
  161. killCamText.text = $"{currentKillCamData.killerName} killed {currentKillCamData.victimName}";
  162.  
  163.  
  164.  
  165. yield return new WaitForSeconds(killCamDuration);
  166.  
  167.  
  168.  
  169. // Transition back to main camera
  170.  
  171. yield return StartCoroutine(CameraTransition(mainCamera.transform.position, mainCamera.transform.rotation, true));
  172.  
  173.  
  174.  
  175. // Fade in main camera
  176.  
  177. if (fadeCoroutine != null)
  178.  
  179. {
  180.  
  181. StopCoroutine(fadeCoroutine);
  182.  
  183. }
  184.  
  185. fadeCoroutine = StartCoroutine(FadeScreen(false));
  186.  
  187.  
  188.  
  189. // Clean up KillCam
  190.  
  191. CleanUpKillCam();
  192.  
  193.  
  194.  
  195. isKillCamActive = false;
  196.  
  197. }
  198.  
  199.  
  200.  
  201. private void SetUpKillCam()
  202.  
  203. {
  204.  
  205. mainCamera.gameObject.SetActive(false);
  206.  
  207. killCam.gameObject.SetActive(true);
  208.  
  209. killCamCanvas.enabled = true;
  210.  
  211.  
  212.  
  213. // Initialize kill cam position and rotation
  214.  
  215. killCam.transform.position = currentKillCamData.killerPosition;
  216.  
  217. killCam.transform.rotation = currentKillCamData.killerRotation;
  218.  
  219. }
  220.  
  221.  
  222.  
  223. private void StartKillCamEffect()
  224.  
  225. {
  226.  
  227. if (killCamEffectPrefab != null)
  228.  
  229. {
  230.  
  231. killCamEffectInstance = Instantiate(killCamEffectPrefab, killCam.transform);
  232.  
  233. // Optionally configure the effect
  234.  
  235. }
  236.  
  237. }
  238.  
  239.  
  240.  
  241. private void CleanUpKillCam()
  242.  
  243. {
  244.  
  245. if (killCamEffectInstance != null)
  246.  
  247. {
  248.  
  249. Destroy(killCamEffectInstance);
  250.  
  251. }
  252.  
  253.  
  254.  
  255. killCamCanvas.enabled = false;
  256.  
  257. killCam.gameObject.SetActive(false);
  258.  
  259. mainCamera.gameObject.SetActive(true);
  260.  
  261. }
  262.  
  263.  
  264.  
  265. private IEnumerator CameraTransition(Vector3 targetPosition, Quaternion targetRotation, bool reverse = false)
  266.  
  267. {
  268.  
  269. float elapsedTime = 0f;
  270.  
  271. Vector3 startPosition = reverse ? killCam.transform.position : mainCamera.transform.position;
  272.  
  273. Quaternion startRotation = reverse ? killCam.transform.rotation : mainCamera.transform.rotation;
  274.  
  275.  
  276.  
  277. while (elapsedTime < transitionDuration)
  278.  
  279. {
  280.  
  281. float t = elapsedTime / transitionDuration;
  282.  
  283. if (reverse)
  284.  
  285. {
  286.  
  287. killCam.transform.position = Vector3.Lerp(startPosition, targetPosition, t);
  288.  
  289. killCam.transform.rotation = Quaternion.Slerp(startRotation, targetRotation, t);
  290.  
  291. }
  292.  
  293. else
  294.  
  295. {
  296.  
  297. mainCamera.transform.position = Vector3.Lerp(startPosition, targetPosition, t);
  298.  
  299. mainCamera.transform.rotation = Quaternion.Slerp(startRotation, targetRotation, t);
  300.  
  301. }
  302.  
  303. elapsedTime += Time.deltaTime;
  304.  
  305. yield return null;
  306.  
  307. }
  308.  
  309.  
  310.  
  311. if (reverse)
  312.  
  313. {
  314.  
  315. killCam.transform.position = targetPosition;
  316.  
  317. killCam.transform.rotation = targetRotation;
  318.  
  319. }
  320.  
  321. else
  322.  
  323. {
  324.  
  325. mainCamera.transform.position = targetPosition;
  326.  
  327. mainCamera.transform.rotation = targetRotation;
  328.  
  329. }
  330.  
  331. }
  332.  
  333.  
  334.  
  335. private IEnumerator FadeScreen(bool fadeOut)
  336.  
  337. {
  338.  
  339. float fadeDuration = 1.0f;
  340.  
  341. float fadeSpeed = 1.0f / fadeDuration;
  342.  
  343. float alpha = fadeOut ? 0 : 1;
  344.  
  345. float targetAlpha = fadeOut ? 1 : 0;
  346.  
  347.  
  348.  
  349. while (!Mathf.Approximately(alpha, targetAlpha))
  350.  
  351. {
  352.  
  353. alpha = Mathf.MoveTowards(alpha, targetAlpha, fadeSpeed * Time.deltaTime);
  354.  
  355. fadeImage.color = new Color(0, 0, 0, alpha);
  356.  
  357. yield return null;
  358.  
  359. }
  360.  
  361. }
  362.  
  363.  
  364.  
  365. private void ApplyCameraShake()
  366.  
  367. {
  368.  
  369. if (shakeIntensity > 0)
  370.  
  371. {
  372.  
  373. Vector3 shake = Random.insideUnitSphere * shakeIntensity;
  374.  
  375. killCam.transform.position += shake;
  376.  
  377. }
  378.  
  379. }
  380.  
  381.  
  382.  
  383. public struct KillCamData
  384.  
  385. {
  386.  
  387. public Vector3 killerPosition;
  388.  
  389. public Quaternion killerRotation;
  390.  
  391. public string killerName;
  392.  
  393. public string victimName;
  394.  
  395. }
  396.  
  397. }
  398.  
  399.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement