Day 36 | Camera Shake: 2D Space Shooter Series 04

In this article, I’ll go over how I added a camera shake when the player takes damage.

Existing Sources for Camera Shake

Honestly, when I was learning how to implement a camera shake, one of the best sources I found was this article:

It explains that the main ways to implement the camera shake are through scripts, animation, and plugins. The article also gives clear examples of each. Therefore, in this article, we’ll actually go over how to create a camera shake using Unity’s Cinemachine.

Camera Shake with Cinemachine

Assuming Cinemachine is installed, there are 4 steps to follow to implement a camera shake.

  • First, you’ll need to add a Cinemachine Brain component to the Main Camera.
  • Second, you’ll need to add a Virtual Camera. You can do this by going to Cinemachine -> Create Virtual Camera. Then, you’ll add a Cinemachine Impulse Listener component. In the component, since this is for a 2D game, we’ll want to check the Use 2D Distance option.
  • Third, you’ll create an empty GameObject called Shake. You’ll add a Cinemachine Impulse Source component and a custom script that we’ll call CameraShakeScript.
  • We’ll use the Impulse Source component to shape our camera shake. For the property called Raw Signal, we’ll create a CinemachineFixedSignal by going to Create > Cinemachine > Fixed Signal Definition in the Project Hierarchy. In the Signal, we can define how we want our camera to shake by editing the signal shape on different axes.
  • Finally, in our script, we’ll access the Impulse Source component and tell it to GenerateImpulse(), which will send a signal to our camera to shake. After shaking for a defined amount of time, reset the camera position to be where you need it.

Now, you can refer to this script from other scripts and use the public method Shake() to trigger the camera shake effect. Now, whenever the player loses health, I refer to this script to create the shake effect.