Screen Shake in Unity

Time to add some extra excitement to our game. When our ship is hit, we want the screen to shake. Screen shake in Unity is nothing more than a simple animation applied to the camera and triggered on demand. Let’s dive in…

Here’s our completed effect:

We’re going to create two animations for the shake effect. One for our idle position, and one for the shake. Then we will just need to trigger the animation to turn it on and let the animator return the state to the idle animation when the shake animation is complete. Like this:

Let’s start by adding an idle animation to the main camera. Select the Main Camera, select the animation tab to open the Animation window, and click the Create button. Name it Idle. Turn on record with the little red button and set the Main Camera position to it’s default position to get our frame settings. Turn off record, and let’s create a new clip using the clip drop-down.

Name this one Shake. Now turn on record and let’s move our timeline around while we tweak the rotation settings on the camera. A good interval is .02 seconds. So at 0:00 we have no change. At 0:02 we slightly rotate the camera. At 0:04 we go back to the default. At 0:06 we slightly rotate in the opposite direction. Then at 0:08 we go back to the default. Turn off record.

Now for the Trigger. In the Animator window, switch to the Parameters tab and use the drop-down to add a new Trigger:

Name it shake. Now right-click on the Idle animation and add a transition to the Shake animation:

Select the Transition Arrow and in the Inspector add the shake condition and uncheck Has Exit Time and set Transition Duration to 0:

Now make a Transition going from shake to idle. Set the Exit Time to 1 and Transition Duration to 0. Then select the Shake animation in the Animations folder in the Project view and turn off Loop Time in the inspector.

Ok, we’re almost done.

Now we’re going to create a Game Object to manager our shake and we will also need a scripted object that sets the trigger on our animation. Create an empty object called ShakeManager then create a new C# script called Shake and drop it on our ShakeManager. In the new Shake script, create a variable of type Animator. Now create a new method and inside the method called CamShake(), set the trigger to our ‘shake’ trigger:

Now whenever we want the screen to shake, we can just call CamShake().

To call CamShake() well need a reference to the Game Object we added the script to. Let’s give the ShakeManager a tag of “ScreenShake” and drag the Main Camera into the CamAnim animator variable from our script:

Now we can get the reference to Shake component on the ShakeManager in the Start() method (on our Player in this case) by referencing the tag:

And finally, let’s call CamShake() anytime our player incurs damage to activate the ‘shake’ trigger and play our animation:

--

--

--

Unity and Flutter developer interested in creating mixed-reality applications.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Loyalty Store End-User License Agreement

The Ultimate Git & GitHub Cheatsheet

Dynamic Delivery in Android — Part 2

How to trim leading and trailing Whitespace from a Text String in C# .NET Framework

Ruby: A Jewel In Your List of Programming Languages

First Unique Letter JS

Agile is Dead … Long Live Agile!

Is it bad to quit drinking coffee cold turkey

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Richard Morgan

Richard Morgan

Unity and Flutter developer interested in creating mixed-reality applications.

More from Medium

Unity and Object movement

Tessellation in HDRP

Framing Transposer Virtual Camera Explained in Unity’s Cinemachine

Installing the Universal Render Pipeline in Unity 2021.2