How to use Cinemachinie Virtual Camera Extensions: Camera Shake with Impulse Listener
A well timed camera shake can really add a lot of player immersion to your game. Rather than using a custom camera shake script, let’s take a look at using cinemachine to shake the camera with an impulse listener. If you want to read about writing a custom camera shake script, please check out my article here https://medium.com/nerd-for-tech/refactoring-the-camera-shake-in-unity2d-628664c6f871,
The next cinemachine extension in this series is the Impulse Listener. Just navigate down to the Extensions option on your virtual camera and select the Impulse Listener option to add the script component.
The attached script needs a Raw Signal input to be assigned, under the Signal Shape header.
Create a new Noise Settings in your Project folder by navigating to Cinemachine>Noise Settings.
Per the Unity documentation, the ‘Noise Settings generates Basic Multi-Channel Perlin noise for Cinemachine Virtual Cameras’. You can ‘add noise in up to six dimensions, including three rotational and positional axes’.
For this example I will only be adding positional noise, so I expand the drop-down for each axis(X, Y, Z). Under Components where it says List is Empty, click the plus icon to add Frequency and Amplitude components.
The X-axis is assigned Frequency(shake speed) and Amplitude(shake intensity) values of 1. The Red line represents the noise wave on the X, while the Position Noise window all the way at the top will show the final mixed values if all three axis inputs.
The tick box on the bottom right can be enabled if you want your wave to be non-random.
Being that these waves will move the camera on different axes to make a camera shake effect, I leave all of the waves random, and assign them different values to create a more diverse path of motion.
When you are finished setting up the Noise Settings, assign it to the Raw Signal in the inspector. There are additional Amplitude and Frequency settings here that will control your overall signal.
In the Time Envelope dropdown, you have some additional options. The Attack is the time in seconds to build the shake from no movement, to full shake. The Sustain Time dictates how long the shake will occur at it’s maximum intensity . The Decay is the time that the shake moves from max to no shake. If desired, you can use curves to influence your Attack and Decay times. The Scale With Impact option will make a larger explosion longer, and a smaller explosion shorter.
The next thing we need is a script to trigger the camera shake with the impulse listener.
This script could go on the virtual camera, or an empty object in your scene to hold the impulse. I use option two here.
You first need to be using the Cinemachine Namespace. Then a variable can be added for a Cinemachine Impulse Source. The impulse source is initiated in void Start. For this example, I am checking the left mouse button in update, and calling a method(Shake) to tell the impulse source to call the Generate Impulse method.
That’s it! The mouse button triggers a noise impulse on the camera, and the camera moves according to the waves. Please join me in my next article where we talk about shaking the camera on collision, and thanks for reading!