From 6ce8b9e22fc13be34b442c7b6af48b42cd44275a Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Wed, 13 Mar 2024 11:00:58 +0800 Subject: +init --- UnitySA.Utility/FOVZoom.cs | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 UnitySA.Utility/FOVZoom.cs (limited to 'UnitySA.Utility/FOVZoom.cs') diff --git a/UnitySA.Utility/FOVZoom.cs b/UnitySA.Utility/FOVZoom.cs new file mode 100644 index 0000000..555f205 --- /dev/null +++ b/UnitySA.Utility/FOVZoom.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections; +using UnityEngine; + +namespace UnitySA.Utility; + +[Serializable] +public class FOVZoom +{ + public Camera Camera; + + [HideInInspector] + public float originalFov; + + public float FOVIncrease = 3f; + + public float TimeToIncrease = 1f; + + public float TimeToDecrease = 1f; + + public AnimationCurve IncreaseCurve; + + public void Setup(Camera camera) + { + CheckStatus(camera); + Camera = camera; + originalFov = camera.fieldOfView; + } + + private void CheckStatus(Camera camera) + { + if (camera == null) + { + throw new Exception("FOVKick camera is null, please supply the camera to the constructor"); + } + if (IncreaseCurve == null) + { + throw new Exception("FOVKick Increase curve is null, please define the curve for the field of view kicks"); + } + } + + public void ChangeCamera(Camera camera) + { + Camera = camera; + } + + public IEnumerator FOVKickUp() + { + float t = Mathf.Abs((Camera.fieldOfView - originalFov) / FOVIncrease); + while (t < TimeToIncrease) + { + Camera.fieldOfView = originalFov + IncreaseCurve.Evaluate(t / TimeToIncrease) * FOVIncrease; + t += Time.deltaTime; + yield return new WaitForEndOfFrame(); + } + } + + public IEnumerator FOVKickDown() + { + float t = Mathf.Abs((Camera.fieldOfView - originalFov) / FOVIncrease); + while (t > 0f) + { + Camera.fieldOfView = originalFov + IncreaseCurve.Evaluate(t / TimeToDecrease) * FOVIncrease; + t -= Time.deltaTime; + yield return new WaitForEndOfFrame(); + } + Camera.fieldOfView = originalFov; + } +} -- cgit v1.1-26-g67d0