-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenShake.cs
71 lines (61 loc) · 1.87 KB
/
ScreenShake.cs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// ScreenShake.cs
// Last edited 5:35 PM 04/21/2015 by Aaron Freedman
using Assets.PrototypingKit.Patterns;
using UnityEngine;
namespace Assets.PrototypingKit
{
public class ScreenShake : Singleton<ScreenShake>
{
private float activeTime;
private Transform camPos;
private float intensity;
private Vector3 originalPosition;
public bool shake;
public bool useLerp;
private float startTime;
private Vector3 target;
//TODO: this screenshake method doesn't work for movable cameras attached to an object
private void Start()
{
originalPosition = Camera.main.gameObject.transform.position;
camPos = GetComponent<Camera>().gameObject.transform;
target = originalPosition;
shake = false;
}
private void Update()
{
if (shake)
{
if (Time.time > startTime + activeTime)
{
StopShake();
}
else
{
Shake();
}
}
camPos.position = useLerp ? Vector3.Lerp(camPos.position, target, 10 * Time.deltaTime) : target;
}
public void Shake()
{
target = new Vector3(originalPosition.x + Random.Range(-intensity, intensity),
originalPosition.y + Random.Range(-intensity, intensity), originalPosition.z);
}
public void StartShake(float time, float _intensity)
{
shake = true;
activeTime = time;
startTime = Time.time;
intensity = _intensity;
}
public void StopShake()
{
ResetPosition();
}
public void ResetPosition()
{
target = originalPosition;
}
}
}