85 lines
3.1 KiB
C#
85 lines
3.1 KiB
C#
|
|
using Unity.Cinemachine;
|
||
|
|
using UnityEngine;
|
||
|
|
using System.Collections;
|
||
|
|
|
||
|
|
public class EagleEyeBehaviour : MonoBehaviour
|
||
|
|
{
|
||
|
|
[SerializeField] private CinemachineCamera virtualCamera;
|
||
|
|
[SerializeField] private CinemachineConfiner2D confiner2D;
|
||
|
|
[SerializeField] private float zoomOutOrthoSize = 30f;
|
||
|
|
[SerializeField] private float normalOrthoSize = 15f;
|
||
|
|
private float currentOrthoSize;
|
||
|
|
[SerializeField] private float transitionDuration = 0.5f; // Duration of the transition
|
||
|
|
[SerializeField] private float eagleEyeDuration = 3f; // Duration to stay zoomed out
|
||
|
|
[SerializeField] private bool disablePlayerInputDuringEagleEye = true; // Gate input disable
|
||
|
|
[SerializeField] private UnityEngine.UI.Button eagleEyeButton; // Reference to the UI button
|
||
|
|
|
||
|
|
private Coroutine zoomCoroutine;
|
||
|
|
|
||
|
|
public void ActivateEagleEye()
|
||
|
|
{
|
||
|
|
//Assigns the Virtual Camera and Confiner if not already assigned
|
||
|
|
if (virtualCamera == null)
|
||
|
|
{
|
||
|
|
virtualCamera = FindAnyObjectByType<CinemachineCamera>();
|
||
|
|
}
|
||
|
|
if (confiner2D == null)
|
||
|
|
{
|
||
|
|
confiner2D = virtualCamera.GetComponent<CinemachineConfiner2D>();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Implementation for activating eagle eye behaviour
|
||
|
|
if (disablePlayerInputDuringEagleEye)
|
||
|
|
{
|
||
|
|
currentOrthoSize = virtualCamera.Lens.OrthographicSize;
|
||
|
|
}
|
||
|
|
if (eagleEyeButton != null)
|
||
|
|
{
|
||
|
|
eagleEyeButton.interactable = false;
|
||
|
|
}
|
||
|
|
if (zoomCoroutine != null) StopCoroutine(zoomCoroutine);
|
||
|
|
zoomCoroutine = StartCoroutine(EagleEyeSequence());
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator EagleEyeSequence()
|
||
|
|
{
|
||
|
|
if (disablePlayerInputDuringEagleEye)
|
||
|
|
{
|
||
|
|
Core.GameManager.Instance.RequestPause(this); // Disable player input
|
||
|
|
}
|
||
|
|
yield return StartCoroutine(SmoothOrthoSize(virtualCamera, zoomOutOrthoSize, transitionDuration));
|
||
|
|
yield return new WaitForSeconds(eagleEyeDuration);
|
||
|
|
float zoomInTarget = disablePlayerInputDuringEagleEye ? currentOrthoSize : normalOrthoSize;
|
||
|
|
yield return StartCoroutine(SmoothOrthoSize(virtualCamera, zoomInTarget, transitionDuration));
|
||
|
|
if (disablePlayerInputDuringEagleEye)
|
||
|
|
{
|
||
|
|
Core.GameManager.Instance.ReleasePause(this); // Re-enable player input
|
||
|
|
}
|
||
|
|
if (eagleEyeButton != null)
|
||
|
|
{
|
||
|
|
eagleEyeButton.interactable = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator SmoothOrthoSize(CinemachineCamera cam, float targetSize, float duration)
|
||
|
|
{
|
||
|
|
float startSize = cam.Lens.OrthographicSize;
|
||
|
|
float elapsed = 0f;
|
||
|
|
while (elapsed < duration)
|
||
|
|
{
|
||
|
|
elapsed += Time.deltaTime;
|
||
|
|
cam.Lens.OrthographicSize = Mathf.Lerp(startSize, targetSize, elapsed / duration);
|
||
|
|
if (confiner2D != null)
|
||
|
|
{
|
||
|
|
confiner2D.InvalidateBoundingShapeCache();
|
||
|
|
}
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
cam.Lens.OrthographicSize = targetSize;
|
||
|
|
if (confiner2D != null)
|
||
|
|
{
|
||
|
|
confiner2D.InvalidateBoundingShapeCache();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|