Skip to content

Instantly share code, notes, and snippets.

@sabotai
Created April 19, 2017 23:29
Show Gist options
  • Save sabotai/904e87555f204ca84ce7a0a91331b564 to your computer and use it in GitHub Desktop.
Save sabotai/904e87555f204ca84ce7a0a91331b564 to your computer and use it in GitHub Desktop.
GD205_ScreenShake
using UnityEngine;
using System.Collections;
public class ScreenShake : MonoBehaviour {
//generic shake script
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public static IEnumerator Shake(float duration, float magnitude) {
Debug.Log ("shaking...");
float elapsed = 0.0f;
Vector3 originalCamPos = Camera.main.transform.position;
while (elapsed < duration) {
elapsed += Time.deltaTime;
float percentComplete = elapsed / duration;
float damper = 1.0f - Mathf.Clamp(4.0f * percentComplete - 3.0f, 0.0f, 1.0f);
// map value to [-1, 1]
float x = Random.value * 2.0f - 1.0f;
float y = Random.value * 2.0f - 1.0f;
x *= magnitude * damper;
y *= magnitude * damper;
Camera.main.transform.position = new Vector3(x + originalCamPos.x, y + originalCamPos.y, originalCamPos.z);
yield return null;
}
Camera.main.transform.position = originalCamPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment