Unity3D: Basic Screen Shake

Anthony and I have done a number of 12-hour game jams over the past year.  In almost every single one, I've used the same screen shake script.  Note that this script works best with a fixed camera; if you have a camera following your player then you would have to make some modifications.

Click to enlarge

Code Download

The code itself is fairly straightforward, so you can download the entire script here.  Note that it has been written and modified during 12-hour game jam sessions, and there is likely a better way to do it, but it certainly works.  If you have any suggestions about enhancements to make, feel free to let me know!


As always, we'll start with initialization.  All variables are private and used internally, and only modified by public functions (which we'll get to later).

The Actual Shake

The actual shaking functionality will take place in the update function.  Note that this script is meant to be placed on the camera, so transform.position refers to the position of the camera.

Calling the Shake

Finally, we have to create some public functions to allow outside scripts to cause the shake.

Another, more general function could take in both the intensity and the number of shakes, and allow the caller to determine how many they want.  However, the above method is nice because it allows us to figure out what a good number for a minor shake is and set it within the function itself, so we don't have to remember it later, or go hunting through old code to figure out what number we used in the past.


And as easy as that, you now have camera shaking in your game!  This can be really useful for selling an impact, and almost any time there's an explosion in one of our game jam games, I usually use at least the Minor Shake.  Again, this script will only work for a fixed camera.  The process is slightly different (although the same concept) for a moving camera.  Perhaps I'll do a quick tutorial on that in the future!