Skip to content

Commit 9b252f9

Browse files
authored
Merge pull request #60 from ForlornU/CameraShake-branch
Rewrite of cameraShake
2 parents 1a555e1 + a60e3cf commit 9b252f9

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

Assets/Scripts/Camera/CameraShake.cs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,66 @@
11
using UnityEngine;
22
using System.Collections;
33

4-
// usage: attach this script into camera, call Shake() method to start
5-
// source: http://answers.unity3d.com/answers/992509/view.html
4+
/*
5+
* Usage: attach this script to a camera or any other object, call Shake() method to start shaking it
6+
* To turn off, change influence to zero
7+
* Attach the camera to an empty game object to keep its local position and rotation
8+
*/
69
namespace UnityLibrary
710
{
811
public class CameraShake : MonoBehaviour
912
{
10-
public bool shakePosition;
11-
public bool shakeRotation;
12-
13-
public float shakeIntensityMin = 0.1f;
14-
public float shakeIntensityMax = 0.5f;
15-
public float shakeDecay = 0.02f;
13+
[Range(0f, 1f)]
14+
public float shakeInfluence = 0.5f;
15+
[Range(0f, 10f)]
16+
public float rotationInfluence = 0f;
1617

1718
private Vector3 OriginalPos;
1819
private Quaternion OriginalRot;
19-
2020
private bool isShakeRunning = false;
2121

22-
// call this function to start shaking
23-
public void Shake()
22+
/// <summary>
23+
/// Will shake the camera with a random value between minIntensity and maxIntensity for duration
24+
/// </summary>
25+
/// <param name="minIntensity"></param>
26+
/// <param name="maxIntensity"></param>
27+
/// <param name="duration"></param>
28+
public void Shake(float minIntensity, float maxIntensity, float duration)
2429
{
30+
if (isShakeRunning)
31+
return;
32+
2533
OriginalPos = transform.position;
2634
OriginalRot = transform.rotation;
27-
StartCoroutine("ProcessShake");
35+
36+
float shake = Random.Range(minIntensity, maxIntensity) * shakeInfluence;
37+
duration *= shakeInfluence;
38+
39+
StartCoroutine(ProcessShake(shake, duration));
2840
}
2941

30-
IEnumerator ProcessShake()
42+
IEnumerator ProcessShake(float shake, float duration)
3143
{
32-
if (!isShakeRunning)
44+
isShakeRunning = true;
45+
float countdown = duration;
46+
float initialShake = shake;
47+
48+
while (countdown > 0)
3349
{
34-
isShakeRunning = true;
35-
float currentShakeIntensity = Random.Range(shakeIntensityMin, shakeIntensityMax);
36-
37-
while (currentShakeIntensity > 0)
38-
{
39-
if (shakePosition)
40-
{
41-
transform.position = OriginalPos + Random.insideUnitSphere * currentShakeIntensity;
42-
}
43-
if (shakeRotation)
44-
{
45-
transform.rotation = new Quaternion(OriginalRot.x + Random.Range(-currentShakeIntensity, currentShakeIntensity) * .2f,
46-
OriginalRot.y + Random.Range(-currentShakeIntensity, currentShakeIntensity) * .2f,
47-
OriginalRot.z + Random.Range(-currentShakeIntensity, currentShakeIntensity) * .2f,
48-
OriginalRot.w + Random.Range(-currentShakeIntensity, currentShakeIntensity) * .2f);
49-
}
50-
currentShakeIntensity -= shakeDecay;
51-
yield return null;
52-
}
53-
54-
isShakeRunning = false;
50+
countdown -= Time.deltaTime;
51+
52+
float lerpIntensity = countdown / duration;
53+
shake = Mathf.Lerp(0f, initialShake, lerpIntensity);
54+
55+
transform.position = OriginalPos + Random.insideUnitSphere * shake;
56+
transform.rotation = Quaternion.Euler(OriginalRot.eulerAngles + Random.insideUnitSphere * shake * rotationInfluence);
57+
58+
yield return null;
5559
}
60+
61+
transform.position = OriginalPos;
62+
transform.rotation = OriginalRot;
63+
isShakeRunning = false;
5664
}
5765
}
5866
}

0 commit comments

Comments
 (0)