Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- public class KillCamManager : MonoBehaviour
- {
- [Header("Camera Settings")]
- public Camera mainCamera;
- public Camera killCam;
- public float killCamDuration = 5.0f;
- public float transitionDuration = 1.0f;
- public float shakeIntensity = 0.5f;
- [Header("UI Elements")]
- public Canvas killCamCanvas;
- public Text killCamText;
- public Image fadeImage;
- public GameObject killCamEffectPrefab;
- private bool isKillCamActive = false;
- private KillCamData currentKillCamData;
- private GameObject killCamEffectInstance;
- private Coroutine fadeCoroutine;
- void Start()
- {
- if (!mainCamera || !killCam || !killCamCanvas || !fadeImage)
- {
- Debug.LogError("KillCamManager: Missing required components.");
- enabled = false;
- return;
- }
- killCamCanvas.enabled = false;
- fadeImage.color = new Color(0, 0, 0, 0);
- }
- void Update()
- {
- if (isKillCamActive)
- {
- ApplyCameraShake();
- }
- }
- public void TriggerKillCam(Vector3 killerPosition, Quaternion killerRotation, string killerName, string victimName)
- {
- if (isKillCamActive) return;
- currentKillCamData = new KillCamData
- {
- killerPosition = killerPosition,
- killerRotation = killerRotation,
- killerName = killerName,
- victimName = victimName
- };
- StartCoroutine(PlayKillCam());
- }
- private IEnumerator PlayKillCam()
- {
- isKillCamActive = true;
- // Fade out main camera
- if (fadeCoroutine != null)
- {
- StopCoroutine(fadeCoroutine);
- }
- fadeCoroutine = StartCoroutine(FadeScreen(true));
- // Set up KillCam
- SetUpKillCam();
- // Start killcam effect
- StartKillCamEffect();
- // Transition to KillCam view
- yield return StartCoroutine(CameraTransition(killCam.transform.position, killCam.transform.rotation));
- killCamText.text = $"{currentKillCamData.killerName} killed {currentKillCamData.victimName}";
- yield return new WaitForSeconds(killCamDuration);
- // Transition back to main camera
- yield return StartCoroutine(CameraTransition(mainCamera.transform.position, mainCamera.transform.rotation, true));
- // Fade in main camera
- if (fadeCoroutine != null)
- {
- StopCoroutine(fadeCoroutine);
- }
- fadeCoroutine = StartCoroutine(FadeScreen(false));
- // Clean up KillCam
- CleanUpKillCam();
- isKillCamActive = false;
- }
- private void SetUpKillCam()
- {
- mainCamera.gameObject.SetActive(false);
- killCam.gameObject.SetActive(true);
- killCamCanvas.enabled = true;
- // Initialize kill cam position and rotation
- killCam.transform.position = currentKillCamData.killerPosition;
- killCam.transform.rotation = currentKillCamData.killerRotation;
- }
- private void StartKillCamEffect()
- {
- if (killCamEffectPrefab != null)
- {
- killCamEffectInstance = Instantiate(killCamEffectPrefab, killCam.transform);
- // Optionally configure the effect
- }
- }
- private void CleanUpKillCam()
- {
- if (killCamEffectInstance != null)
- {
- Destroy(killCamEffectInstance);
- }
- killCamCanvas.enabled = false;
- killCam.gameObject.SetActive(false);
- mainCamera.gameObject.SetActive(true);
- }
- private IEnumerator CameraTransition(Vector3 targetPosition, Quaternion targetRotation, bool reverse = false)
- {
- float elapsedTime = 0f;
- Vector3 startPosition = reverse ? killCam.transform.position : mainCamera.transform.position;
- Quaternion startRotation = reverse ? killCam.transform.rotation : mainCamera.transform.rotation;
- while (elapsedTime < transitionDuration)
- {
- float t = elapsedTime / transitionDuration;
- if (reverse)
- {
- killCam.transform.position = Vector3.Lerp(startPosition, targetPosition, t);
- killCam.transform.rotation = Quaternion.Slerp(startRotation, targetRotation, t);
- }
- else
- {
- mainCamera.transform.position = Vector3.Lerp(startPosition, targetPosition, t);
- mainCamera.transform.rotation = Quaternion.Slerp(startRotation, targetRotation, t);
- }
- elapsedTime += Time.deltaTime;
- yield return null;
- }
- if (reverse)
- {
- killCam.transform.position = targetPosition;
- killCam.transform.rotation = targetRotation;
- }
- else
- {
- mainCamera.transform.position = targetPosition;
- mainCamera.transform.rotation = targetRotation;
- }
- }
- private IEnumerator FadeScreen(bool fadeOut)
- {
- float fadeDuration = 1.0f;
- float fadeSpeed = 1.0f / fadeDuration;
- float alpha = fadeOut ? 0 : 1;
- float targetAlpha = fadeOut ? 1 : 0;
- while (!Mathf.Approximately(alpha, targetAlpha))
- {
- alpha = Mathf.MoveTowards(alpha, targetAlpha, fadeSpeed * Time.deltaTime);
- fadeImage.color = new Color(0, 0, 0, alpha);
- yield return null;
- }
- }
- private void ApplyCameraShake()
- {
- if (shakeIntensity > 0)
- {
- Vector3 shake = Random.insideUnitSphere * shakeIntensity;
- killCam.transform.position += shake;
- }
- }
- public struct KillCamData
- {
- public Vector3 killerPosition;
- public Quaternion killerRotation;
- public string killerName;
- public string victimName;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement