Created
December 6, 2012 03:36
-
-
Save benloong/4221606 to your computer and use it in GitHub Desktop.
Simple CameraShake
This file contains hidden or 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
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