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 --- CameraRecoil.cs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 CameraRecoil.cs (limited to 'CameraRecoil.cs') diff --git a/CameraRecoil.cs b/CameraRecoil.cs new file mode 100644 index 0000000..e82532a --- /dev/null +++ b/CameraRecoil.cs @@ -0,0 +1,49 @@ +using UnityEngine; + +public class CameraRecoil : MonoBehaviour +{ + public Transform yRotation; + + public Transform xRotation; + + private float counter; + + private float lastValue; + + private AnimationCurve usedCurve; + + private float deltaValue; + + private float xRecoil; + + private float yRecoil; + + private bool hasCurve; + + private void Start() + { + } + + private void Update() + { + if (hasCurve && counter < usedCurve.keys[usedCurve.length - 1].time) + { + float num = usedCurve.Evaluate(counter); + counter += Time.deltaTime; + deltaValue = num - lastValue; + lastValue = num; + xRotation.Rotate(Vector3.up * Time.deltaTime * xRecoil * deltaValue, Space.World); + yRotation.Rotate(Vector3.right * Time.deltaTime * yRecoil * deltaValue, Space.Self); + } + } + + public void AddRecoil(Vector2 recoil, AnimationCurve curve) + { + counter = 0f; + lastValue = curve.Evaluate(0f); + usedCurve = curve; + xRecoil = recoil.x * 30f; + yRecoil = recoil.y * -30f; + hasCurve = true; + } +} -- cgit v1.1-26-g67d0