Skip to content

Instantly share code, notes, and snippets.

@benloong
Created December 6, 2012 03:36
Show Gist options
  • Save benloong/4221606 to your computer and use it in GitHub Desktop.
Save benloong/4221606 to your computer and use it in GitHub Desktop.
Simple CameraShake
function CameraShake () {
var hitTime = Time.time;
var originalPosition = cam.transform.localPosition.x;
var shakeCounter = numberOfShakes;
var shakeDistance = startingShakeDistance;
while (shakeCounter > 0) {
// Make timers always start at 0
var timer = (Time.time - hitTime) * shakeSpeed;
cam.transform.localPosition.x = originalPosition + Mathf.Sin(timer) * shakeDistance;
// See if we've gone through an entire sine wave cycle, reset distance timer if so and do less distance next cycle
if (timer > Mathf.PI * 2.0) {
hitTime = Time.time;
shakeDistance *= decreasePercentage;
shakeCounter--;
}
yield;
}
cam.transform.localPosition.x = originalPosition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment