5SD037, Big Game Project, Programming

Screenshakes

I created an asset where the player has to trigger a button to open a heavy stone door. To give it the player the feeling that the door is really old and heavy I decided to add screenshakes to increase the effect.

Knowing that this probably won’t be the only situation in the game where screenshakes are useful I wanted the script to be versatile and easily modified for many different instances.

1310q2.gif

 

Basic function

The script is added to the camera and modifies its position each frame. I use Perlin noise to get a randomized value on all three axes that is added on top of the cameras position.
If the camera is at position (5, 5, -10) in the world space the screenshake adds a random number within a specified range. For example, it generates (0.2, -0.3, 0.4). Added to the cameras original position on that frames gives the camera position (5.2, 4.7, -9.6). This is done each frame with new randomized values each time.

Optional settings

To make it more versatile  I wrote a method to trigger a single shake with three overloads each adding a little more options.screenshakeimage

 

 

 

 

 

 

 

Offset range: This parameter specifies maximum values to be added to the camera position.
Shake time: How long in seconds the shake is.
Shake speed: How fast the a random position is generated. Higher = More vibrating.
Time scale: This sets the global timescale. Making it possible for slow motion effects.
Smooth speed and range: The speed and range offset decreases by time, resulting in an effect where the shakes become less and less intense towards the end of the shake.

To make fine tuning easier and faster I added a debugging method where you can assign a key that triggers a screenshake with the public variables in the inspector.

Sometimes you don’t know exactly how long a shake should be since it might be dependent on some event that varies in length. That’s why also added a method that can be called on a frame-by-frame basis, and doesn’t take time as a parameter. A stop method was also made that cancels any currently playing screenshakes.

 

 

Standard

2 reaktioner på ”Screenshakes

Lämna en kommentar