Camera Shake in Unity


After much Googleing for how to do a camera shaking effect in Unity, I decided to write my own small script to accomplish the effect. It combines both positional movement with rotational movement to simulate closely an actual camera shake.

The two key variables are:

shake_intensity

Shake_intensity determines the initial intensity of the shaking — how much variance to allow in the camera position.

shake_decay

Shake_decay controls is the amount that shake_intensity is decremented each update. It determines if the shake is long or short.

Unity Web Player. Install now!

Posted in Uncategorized
  • Sfsdf

    This is so cool! A really simple idea, that will get great results in-game, I’m sure. Nice one! And thanks for sharing your methods.

  • http://www.facebook.com/Zerox911 Chester Liew

     hi.. how to stop the camera from moving from its original position..??

    seems like when i pressed the “shake” button a lot of times.. the camera slowly moves while vibrating..

    is it possible to return the camera to its original place after vibrating..??
    and how to do that..

    thank you..

    • mdjasper

      Great question. I see what you mean. The reason that happens is that when you press shake it grabs the initial position and rotation to return it to when finished. However, when you press it quickly, the second press will grab what it thinks is the initial position – but in fact is the position midway through the first shake.

      The way to solve this is to remove the “grab the original position” bit, and always reset it to Vector3(0,0,0). Then put the camera inside another object as a child, and control the movement through the parent object.

      I will be updating the script and demo to reflect these changes.

      • http://www.facebook.com/Zerox911 Chester Liew

        where exactly is this “grab the original position” in the script that you have stated..??

        what i understood from you reply is..

        i have to somehow remove this “original position” variable to stop it from moving, resetting it in some kind of “function” and make my camera as a child to an empty gameobject to set the position.. am i correct..??

        one more request…

        where should i remove the “original position” and how do i reset the position..

        thank you

      • Aiden

        Might I suggest some sort of boolean check to see– when you press a/the shake button– if it’s already in the process of shaking, it skips (re)acquiring the initial position. Instead, it simply uses the one already defined in the first shake. Some properly scoped variable called “is_shaking” that is set to 1 when the function initiates, and 0 when the function has completed its duty. So if the function is called and the is_shaking variable is 0, it acquires the original position. If it’s 1, it does not, and simply uses values that were previously acquired. In this way, the camera shaking can be initiated at any axis/rotation, instead of having to rely on a pre-defined “Vector3(0,0,0)”

  • Daniel

    Thanks!!!

  • vasant

    Thank you, was looking for this for a while

  • andreadst

    Thank you for this! I’ve been using it in a small prototype and it works great :)

  • http://twitter.com/aerinonfire kumavis

    dat shake

Sign up for the super spectacular newsletter
You might also like: