diff options
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; + } +} |