diff options
author | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
commit | 6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch) | |
tree | b38119d2acf0a982cb67e381f146924b9bfc3b3f /CameraRecoil.cs |
+init
Diffstat (limited to 'CameraRecoil.cs')
-rw-r--r-- | CameraRecoil.cs | 49 |
1 files changed, 49 insertions, 0 deletions
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; + } +} |