Adding Camera Shake using Cinemachine: GDJ 20

Brett Blandford
3 min readJun 5, 2021

Adding a camera shake when the player gets hit is another great way to visualize that the ship getting hurt and immerse the player further. I wanted variety in my camera shakes and after some research Cinemachine seemed like the best approach.

Cinemachine can be downloaded from the Package Manager. The first thing to do after downloading Cinemachine is to add a CinemachineBrain component to the Main Camera. This in simple terms chooses which Cinemachine virtual camera controls the Main Camera.

Now it is time to create a Cinemachine virtual camera. This is done just like any other game object. On this virtual camera there would be a component named “CinemachineVirtualCamera” which has a section called noise. Set the noise to “Basic Multi Channel Perlin” and set the Noise Profile to 6D Shake. It should look lie this:

Amplitude affects the size of the camera shake while frequency affects the speed of the camera shake. For my purposes, I only want to change the amplitude of the camera shake.

Next a CameraShake script is created an added to the virtual camera. The noise needs to be accessed in this script so the Amplitude can be adjusted. This is one way it could be accomplished.

To make the camera shake and a specific moment, the player getting hit, this script need a public method that can be called in other scripts that makes the camera shake for a certain amount of time. Here it what that method and the rest of the CameraShake script looks like:

The ShakeCamera() method takes two values, one for the intensity of the shake and another for the duration. This intensity is then set to the amplitude through _noise.m_AmplitudeGain. Then time is set to shaketime which is a global variable. This variable is subtracted from using Time.deltaTime and when the shakeTime equals 0, the camera shake ends. For example, ShakeCamera(1f, .5f) will shake the camera at an intensity of 1 for half a second.

And now there is camera shake! Here is how it looks but it is at a pretty low frame rate because of the gif recording:

End of GDJ 20!

--

--