132 lines
4.8 KiB
C#
132 lines
4.8 KiB
C#
using System.Collections;
|
|
using Unity.Cinemachine;
|
|
using UnityEngine;
|
|
|
|
namespace DamianExperiments
|
|
{
|
|
public class EagleEyeBehaviour : MonoBehaviour
|
|
{
|
|
// Serialized backing fields allow manual assignment in the inspector
|
|
private GameObject _cinecameraObject;
|
|
private CinemachineCamera _virtualCamera;
|
|
private CinemachineConfiner2D _confiner2D;
|
|
|
|
// Lazy-fetched properties: if null, try to find the GameObject tagged "MainCinemachineCamera"
|
|
private CinemachineCamera VirtualCamera
|
|
{
|
|
get
|
|
{
|
|
if (_virtualCamera == null)
|
|
{
|
|
if (_cinecameraObject == null)
|
|
_cinecameraObject = GameObject.FindWithTag("MainCinemachineCamera");
|
|
if (_cinecameraObject != null)
|
|
{
|
|
_virtualCamera = _cinecameraObject.GetComponent<CinemachineCamera>();
|
|
|
|
if (_virtualCamera == null)
|
|
Debug.LogWarning("EagleEyeBehaviour: Found object with tag 'MainCinemachineCamera' " +
|
|
"but couldn't find a CinemachineCamera component.");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("EagleEyeBehaviour: No GameObject found with tag 'MainCinemachineCamera'.");
|
|
}
|
|
}
|
|
|
|
return _virtualCamera;
|
|
}
|
|
set => _virtualCamera = value;
|
|
}
|
|
|
|
private CinemachineConfiner2D Confiner2D
|
|
{
|
|
get
|
|
{
|
|
if (_confiner2D == null)
|
|
{
|
|
// If a virtual camera exists, try to pull the confiner from it
|
|
if (VirtualCamera != null)
|
|
{
|
|
_confiner2D = VirtualCamera.GetComponent<CinemachineConfiner2D>();
|
|
}
|
|
|
|
if (_confiner2D == null)
|
|
{
|
|
Debug.LogWarning("EagleEyeBehaviour: CinemachineConfiner2D not found on the MainCinemachineCamera object.");
|
|
}
|
|
}
|
|
|
|
return _confiner2D;
|
|
}
|
|
set => _confiner2D = value;
|
|
}
|
|
|
|
[SerializeField] private float zoomOutOrthoSize = 30f;
|
|
[SerializeField] private float normalOrthoSize = 15f;
|
|
[SerializeField] private float transitionDuration = 0.5f; // Duration of the transition
|
|
[SerializeField] private float eagleEyeDuration = 3f; // Duration to stay zoomed out
|
|
[SerializeField] private UnityEngine.UI.Button eagleEyeButton; // Reference to the UI button
|
|
|
|
private Coroutine _zoomCoroutine;
|
|
private Coroutine _smoothOrthoCoroutine;
|
|
private float _currentOrthoSize;
|
|
|
|
public void ResetEagleEye()
|
|
{
|
|
if (_zoomCoroutine != null)
|
|
StopCoroutine(_zoomCoroutine);
|
|
if (_smoothOrthoCoroutine != null)
|
|
StopCoroutine(_smoothOrthoCoroutine);
|
|
if (eagleEyeButton != null)
|
|
eagleEyeButton.interactable = true;
|
|
}
|
|
|
|
|
|
public void ActivateEagleEye()
|
|
{
|
|
if (eagleEyeButton != null)
|
|
{
|
|
eagleEyeButton.interactable = false;
|
|
}
|
|
if (_zoomCoroutine != null) StopCoroutine(_zoomCoroutine);
|
|
_zoomCoroutine = StartCoroutine(EagleEyeSequence());
|
|
}
|
|
|
|
private IEnumerator EagleEyeSequence()
|
|
{
|
|
_smoothOrthoCoroutine = StartCoroutine(SmoothOrthoSize(VirtualCamera, zoomOutOrthoSize, transitionDuration));
|
|
yield return _smoothOrthoCoroutine;
|
|
yield return new WaitForSeconds(eagleEyeDuration);
|
|
float zoomInTarget = normalOrthoSize;
|
|
_smoothOrthoCoroutine = StartCoroutine(SmoothOrthoSize(VirtualCamera, zoomInTarget, transitionDuration));;
|
|
yield return _smoothOrthoCoroutine;
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|