Screen Shake Effect

Discussion in 'Scripting' started by liquidgraph, May 12, 2009.

  1. liquidgraph

    liquidgraph

    New Member

    Joined:
    Feb 26, 2009
    Messages:
    324
    Has anyone out there made a screen shake effect for the camera and know the best way to implement it? I want this effect for something like an earthquake, a large explosion, or a heavy impact. Ideas?
  2. Jacob Williams

    Jacob Williams

    Member

    Joined:
    Jan 30, 2009
    Messages:
    209
    I accomplished this in a very easy way. If you have access to a 3D Animation app, just animated an object (a simple cube, for instance) shaking and moving a bit randomly. In unity, make the camera a child of the object you animated and turn off the renderer. Whenever you want a camera shake, just play the animation.

    -cD
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    25,179
    This project has a camera shake routine in it. Using code instead of an animation means you can dynamically change the intensity and so on.

    --Eric
  4. liquidgraph

    liquidgraph

    New Member

    Joined:
    Feb 26, 2009
    Messages:
    324
    Thanks guys, two unique solutions! I would prefer code if it produced an effect to my liking. I'll have to test this out.
  5. Martin Schultz

    Martin Schultz

    Member

    Joined:
    Jan 10, 2006
    Messages:
    1,375
    I'm using a quite simple approach. I linked the main camera to an empty game object and if wanted to shake the camera, I set in each update a random local position of the attached camera within a unit sphere:

    http://unity3d.com/support/documentation/ScriptReference/Random-insideUnitSphere.html

    So it could look like this (semi pseudo code):

    Code (csharp):
    1. var camera : Camera; // set this via inspector
    2. var shake : float = 0;
    3. var shakeAmount : float = 0.7;
    4. var decreaseFactor : float = 1.0;
    5.  
    6. function Update() {
    7.   if (shake > 0) {
    8.     Camera.transform.localPosition = Random.insideUnitSphere * shakeAmount;
    9.     shake -= Time.deltaTime * decreaseFactor;
    10.  
    11.   } else {
    12.     shake = 0.0;
    13.   }
    14. }
    (all off head, have the complete function at home if you want it)

    That function then shakes the camera locally around in each frame and decreases over time. So if you want to shake it, simply set "shake" to 1.0 or 2.0 or something like that and it starts to shake. I use the effect in my new "Decane intro" for all my games when the car crashes into my Decane logo. See the intro here:

    http://www.wooglie.com/playgame.php?gameID=28

    - Martin
  6. Srbhunter

    Srbhunter

    New Member

    Joined:
    Feb 25, 2014
    Messages:
    2
    Thanks Martin, very simple solution and it's working great!
  7. theLucre

    theLucre

    Member

    Joined:
    Jan 20, 2014
    Messages:
    16
    Awesome solution, Martin. Thank you!
  8. cmcpasserby

    cmcpasserby

    Member

    Joined:
    Jul 18, 2014
    Messages:
    54
    also a other good option is doing something similar to what martin did, but using source that provides a smooth random like a perlin to get your random values, which i find looks a little less glitch and more like a shaky came in real-life than using truly random values.