summaryrefslogtreecommitdiff
path: root/_Camera/CameraRecoil.cs
blob: e82532a203d492cfee1beff75a9db815208d113b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
	}
}