Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Created October 24, 2014 03:24
Show Gist options
  • Save grimmdev/0257f564c09a9f92fd2f to your computer and use it in GitHub Desktop.
Save grimmdev/0257f564c09a9f92fd2f to your computer and use it in GitHub Desktop.
Simple to use, adds the ability to shake your Camera, with little to no effort, includes 3 constructors.
using UnityEngine;
using System.Collections;
public class CameraShake : MonoBehaviour{
private static Vector3 originPosition;
private static Quaternion originRotation;
private static float shakeDecay = 0.002f;
private static float shakeIntensity;
public static IEnumerator Shake(Transform t){
originPosition = t.position;
originRotation = t.rotation;
shakeIntensity = 0.3f;
while (shakeIntensity > 0) {
t.position = originPosition + Random.insideUnitSphere * shakeIntensity;
t.rotation = new Quaternion (
originRotation.x + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.y + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.z + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.w + Random.Range (-shakeIntensity, shakeIntensity) * .2f);
shakeIntensity -= shakeDecay;
yield return false;
}
}
public static IEnumerator Shake(Transform t, float i){
originPosition = t.position;
originRotation = t.rotation;
shakeIntensity = i;
while (shakeIntensity > 0) {
t.position = originPosition + Random.insideUnitSphere * shakeIntensity;
t.rotation = new Quaternion (
originRotation.x + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.y + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.z + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.w + Random.Range (-shakeIntensity, shakeIntensity) * .2f);
shakeIntensity -= shakeDecay;
yield return false;
}
}
public static IEnumerator Shake(Transform t, float i, float d){
originPosition = t.position;
originRotation = t.rotation;
shakeIntensity = i;
while (shakeIntensity > 0) {
t.position = originPosition + Random.insideUnitSphere * shakeIntensity;
t.rotation = new Quaternion (
originRotation.x + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.y + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.z + Random.Range (-shakeIntensity, shakeIntensity) * .2f,
originRotation.w + Random.Range (-shakeIntensity, shakeIntensity) * .2f);
shakeIntensity -= d;
yield return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment