Created
January 19, 2018 14:27
-
-
Save oultrox/0b8b5932c42e1782c4108c57d6249f58 to your computer and use it in GitHub Desktop.
ManagerCamShake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ManagerCamShake : MonoBehaviour | |
{ | |
public static ManagerCamShake instance; | |
[SerializeField] private float speed = 20f; | |
[SerializeField] private AnimationCurve damper = new AnimationCurve(new Keyframe(0f, 1f), new Keyframe(0.9f, .33f, -2f, -2f), new Keyframe(1f, 0f, -5.65f, -5.65f)); | |
private float shakenessIntensity = 1; | |
private Transform originalTransform; | |
private Vector3 originalPos; | |
private Quaternion originalRot; | |
private void Awake() | |
{ | |
if (instance == null) | |
{ | |
instance = this; | |
} | |
else if (instance != this) | |
{ | |
Destroy(gameObject); | |
} | |
originalTransform = this.transform; | |
} | |
/// <summary> | |
/// Shakes the camera moving it's transform's position. | |
/// </summary> | |
public void StartShake(float _duration, float _magnitude) | |
{ | |
_magnitude *= shakenessIntensity; | |
StopAllCoroutines(); | |
originalPos = originalTransform.localPosition; | |
StartCoroutine(ShakePosition(_duration, _magnitude, damper)); | |
} | |
IEnumerator ShakePosition(float duration, float magnitude, AnimationCurve damper = null) | |
{ | |
float elapsed = 0f; | |
while (elapsed < duration) | |
{ | |
elapsed += Time.deltaTime; | |
float damperedMag = (damper != null) ? (damper.Evaluate(elapsed / duration) * magnitude) : magnitude; | |
float x = (Mathf.PerlinNoise(Time.time * speed, 0f) * damperedMag) - (damperedMag / 2f); | |
float y = (Mathf.PerlinNoise(0f, Time.time * speed) * damperedMag) - (damperedMag / 2f); | |
transform.localPosition = new Vector3(originalPos.x + x, originalPos.y + y, originalPos.z); | |
yield return null; | |
} | |
transform.localPosition = originalPos; | |
} | |
/// <summary> | |
/// Shakes the camera rotating it's transform. | |
/// </summary> | |
public void StartShakeRotating(float _duration, float _magnitude) | |
{ | |
StopAllCoroutines(); | |
originalRot = originalTransform.localRotation; | |
StartCoroutine(ShakeRotation(_duration, _magnitude, damper)); | |
} | |
IEnumerator ShakeRotation(float duration, float magnitude, AnimationCurve damper = null) | |
{ | |
Vector3 originalEuler = originalRot.eulerAngles; | |
float elapsed = 0f; | |
while (elapsed < duration) | |
{ | |
elapsed += Time.deltaTime; | |
float damperedMag = (damper != null) ? (damper.Evaluate(elapsed / duration) * magnitude) : magnitude; | |
float x = (Mathf.PerlinNoise(Time.time * speed, 0f) * damperedMag) - (damperedMag / 2f); | |
float y = (Mathf.PerlinNoise(0f, Time.time * speed) * damperedMag) - (damperedMag / 2f); | |
float z = (Mathf.PerlinNoise(0.5f, Time.time * speed * 0.5f) * damperedMag) - (damperedMag / 2f); | |
originalTransform.localRotation = Quaternion.Euler(new Vector3(originalEuler.x + x, originalEuler.y + y, originalEuler.z + z)); | |
yield return null; | |
} | |
originalTransform.localRotation = originalRot; | |
} | |
#region Properties | |
public float ShakenessIntensity | |
{ | |
get | |
{ | |
return shakenessIntensity; | |
} | |
set | |
{ | |
shakenessIntensity = value; | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment