← View other scripts
script

Super Easy Screen Shake

A little screen-shake effect can make all the difference, here's a super easy way to add one to your game!
· 2 min read · 1227 views

Screen shake effects are really handy for adding juicy effects to your game - here is my simple, easy to use script to make a screen shake effect in your game!

1
Make the Control Object
Create an Object called ob_Shake

Add the following events:

CREATE

/// @description Set-Up
shakex = 0
shakey = 0
shakeint = 1
shakelen = 1
xs = camera_get_view_x(view_camera[0])
ys = camera_get_view_y(view_camera[0])

STEP

/// @description Shake
if shakelen = 0 then {
shakex = 0
shakey = 0
instance_destroy()}
if shakelen > 0 then {
shakex = -(shakeint/2)+random(shakeint)
shakey = -(shakeint/2)+random(shakeint)
shakelen -= 1 }
camera_set_view_pos(view_camera[0],xs+shakex,ys+shakey)


2
Make the Script
Create a Script called sc_Shake as follows:

/// @function sc_Shake(time,intensity);
/// @param {real} time How long to shake
/// @param {real} intensity How much to shake
function sc_Shake(_time,_intensity){
if instance_number(ob_Shake) = 0 then instance_create_depth(0,0,0,ob_Shake)
    with (ob_Shake) {
    shakelen = _time
    shakeint = _intensity }
}


3
Enable the Camera
This script works by shaking camera & viewport 0, so go in to your room settings and enable them if you haven't already.

image

Get Shakin!

That's it, all set up! Now, whenever you want to add a little screen shake effect, just call the script:

  sc_Shake(<TIME>,<INTENSITY>)

SIDE NOTE: This only works if your camera is not following an object - if your camera IS following an object, edit your code to ensure it is not when the ob_Shake object exists