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/CurveCtrlBob.cs | 50 +++++++++++++++++++++++++++++ UnitySA.Utility/FOVZoom.cs | 69 +++++++++++++++++++++++++++++++++++++++++ UnitySA.Utility/LerpCtrlBob.cs | 39 +++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 UnitySA.Utility/CurveCtrlBob.cs create mode 100644 UnitySA.Utility/FOVZoom.cs create mode 100644 UnitySA.Utility/LerpCtrlBob.cs (limited to 'UnitySA.Utility') diff --git a/UnitySA.Utility/CurveCtrlBob.cs b/UnitySA.Utility/CurveCtrlBob.cs new file mode 100644 index 0000000..04e57e5 --- /dev/null +++ b/UnitySA.Utility/CurveCtrlBob.cs @@ -0,0 +1,50 @@ +using System; +using UnityEngine; + +namespace UnitySA.Utility; + +[Serializable] +public class CurveCtrlBob +{ + public float HorizontalBobRange = 0.33f; + + public float VerticalBobRange = 0.33f; + + public AnimationCurve Bobcurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.5f, 1f), new Keyframe(1f, 0f), new Keyframe(1.5f, -1f), new Keyframe(2f, 0f)); + + public float VerticaltoHorizontalRatio = 1f; + + private float m_CyclePositionX; + + private float m_CyclePositionY; + + private float m_BobBaseInterval; + + private Vector3 m_OriginalCameraPosition; + + private float m_Time; + + public void Setup(Camera camera, float bobBaseInterval) + { + m_BobBaseInterval = bobBaseInterval; + m_OriginalCameraPosition = camera.transform.localPosition; + m_Time = Bobcurve[Bobcurve.length - 1].time; + } + + public Vector3 DoHeadBob(float speed) + { + float x = m_OriginalCameraPosition.x + Bobcurve.Evaluate(m_CyclePositionX) * HorizontalBobRange; + float y = m_OriginalCameraPosition.y + Bobcurve.Evaluate(m_CyclePositionY) * VerticalBobRange; + m_CyclePositionX += speed * Time.deltaTime / m_BobBaseInterval; + m_CyclePositionY += speed * Time.deltaTime / m_BobBaseInterval * VerticaltoHorizontalRatio; + if (m_CyclePositionX > m_Time) + { + m_CyclePositionX -= m_Time; + } + if (m_CyclePositionY > m_Time) + { + m_CyclePositionY -= m_Time; + } + return new Vector3(x, y, 0f); + } +} 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; + } +} diff --git a/UnitySA.Utility/LerpCtrlBob.cs b/UnitySA.Utility/LerpCtrlBob.cs new file mode 100644 index 0000000..9d23469 --- /dev/null +++ b/UnitySA.Utility/LerpCtrlBob.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections; +using UnityEngine; + +namespace UnitySA.Utility; + +[Serializable] +public class LerpCtrlBob +{ + public float BobDuration; + + public float BobAmount; + + private float m_Offset; + + public float Offset() + { + return m_Offset; + } + + public IEnumerator DoBobCycle() + { + float t2 = 0f; + while (t2 < BobDuration) + { + m_Offset = Mathf.Lerp(0f, BobAmount, t2 / BobDuration); + t2 += Time.deltaTime; + yield return new WaitForFixedUpdate(); + } + t2 = 0f; + while (t2 < BobDuration) + { + m_Offset = Mathf.Lerp(BobAmount, 0f, t2 / BobDuration); + t2 += Time.deltaTime; + yield return new WaitForFixedUpdate(); + } + m_Offset = 0f; + } +} -- cgit v1.1-26-g67d0