How to Make the Camera Shake Using Impulse Listener Extensions in Cinemachines Virtual Cameras
Objective: Let’s make the camera shake using Cinemachines Impulse Listener Extension.
Have you ever wanted to create a lightweight camera shake that doesn’t involve much code at all? Then let’s take a look at Cinemachines answer to that!
What is the Impulse Listener Extension in Cinemachine?
This extension gives the virtual camera the ability to ‘listen’ to impulse vibration signals and react to them in a specific way.
Depending on how we set this up, it could be something very basic that provides a very subtle short screen shake when a player falls from a small distance or something as large as an earthquake creating a much larger, lengthier and generally more intense shake as it plays out.
How to Get this Setup
First, let’s add the Impulse Listener Extension to the virtual camera. While having the virtual camera highlighted, scroll down to the bottom of the CinemachineVirtualCamera component and click Extension → Add Extension → Impulse Listener:
Now this component is only looking to hear for the signal noises that are being emitted, so we need to set something up that is going to play the signal noise also.
Let’s Create Some Noise
Create a new empty game object and call it ‘Impulse’.
Next, we are going to add a new component to it called ‘Cinemachine Impulse Source’ which should look like this when it is attached:
Now as you can see we have a small error pop up and that is letting us know that this field is looking for a specific type of asset called a ‘Signal Source Asset’. So let’s create one and get that added in.
Right click in the project window, Create → Cinemachine → Noise Settings. We can now add this asset to the field and start to play around with some of the settings.
Noise Settings Asset
If we open up the Position X and Y by clicking on the small arrows next to them, you can now see that we have these boxes that are looking for components. So let’s hit the ‘+’ button and you will see that it has created 2 new fields and a checkbox. These are the ‘Frequency’, ‘Amplitude’ and ‘Random Wave’. Frequency refers to how often we want the sound waves to happen, while amplitude is how high and low the curves are along the wave. If we were to tick that check box it would give us a perfect wave as shown:
Let’s Do Some More Adjusting of Settings
On the CinemachineImpulseSource component let’s play around with a few of the ‘Time Envelope’ settings:
Attack —This refers to how the signal reaches its full amplitude. This field takes a curve or straight line parameter, defining how it should ramp up to it’s peak amplitude, and how many seconds should it take to get there.
Sustain Time — This refers to how long the shake will sustain itself when it reaches it’s peak amplitude.
Decay — Similar to the attack, it takes a curve or straight line parameter with seconds, telling it how long it should come down from its peak to 0.
Create a Basic Script to Generate an Impulse
Now we just need literally a few lines of code to generate an impulse, using a built in method:
Just simply attach this script to the impulse game object.
Testing
Now, let’s run the game and press the ‘Space’ key and see if the screen shakes!