Understand how an impulse listener works to create your own camera shakes!
Cinemachine Impulse Listener is a system that listens to a specific type of noise, and it is going to place back this noise at a specific interval that you can set. It’s not crazy complicated but there is some work that needs to be done on the back end.
We are currently set up as a 3rd person character with a 3rd person follow. Then we go to extensions and add Cinemachine impulse listener. This is a listener, not the actual script that is going to be running the noise. This is just listening for the noise to come about.
We are going to create a new game object. That will invoke the shake. Next, we add the component, Cinemachine impulse source.
Now we will notice a warning saying it is looking for a new signal source asset, so we need to create one.
create > Cinemachine > NoiseSetting.
If we pull up our X, Y, Z positions, that’s what this is for, a specific axis. We can create a frequency and amplitude along the rotations.
Let’s say we start off with a frequency of 1 and amplitude of 1. You will see wave lengths representing those values.
You can adjust this in the main profile later. There is a button on the end that will make your amplitude even and not random. If you select the button again, they will be random.
We can set this for the Y, and the Z, and you’ll see that it is following the same patter. You don’t really want this; this means that your camera shake is going to be fallowing in the same length. What we want to do is set this so that the X moves at one frequency and the Y moves at another.
So, I’m going to set my Y to 10 and the Z to 3. Now we can see the craziness that is going to be accruing when this noise is triggered.
Then I want to drop this noise setting into our Impulse object we created earlier. This is why I didn’t want to adjust the amplitude and frequency too much because we can fine tune it on these nobs vs. the noise setting itself.
Looking at the impulse control we can adjust the frequency and randomize it so every time it does play it doesn’t start at the same spot.
Time envelope is how long the initial start will occur. Think of it like an earthquake instead of a gunshot. The earthquake can start with a low rumble and rise to a great shake.
The sustain time is how long you want the shake to last at its peak. And the decay you can have it as a curve and have it slop down in the other direction.
With your gunshots you want a longer decay, and you want a longer sustain and attack. Whereas when it comes to a bullet this has to happen really fast, it should be instantaneous.
Scale with impact has to do with the impact radius. If an event is going to be triggered and it’s on another object but it’s still sending a signal it can adjust the intensity depending on how far away it is.
This is going to be how our noise plays back when the event is sent. What we need to do now is create the actual impulse.
We are going to create a camera shake script. Attach this onto the impulse.
First add the Cinemachine library.
Next, we want to grab the impulse source. “Private Cinemachine impulse source _ source;”
Then do a git component.
Now all we want to do is go to the source and generate an impulse. So, we are going to create a function to do this.
Finally, what we want to do is go to the update method and start with an if statement. Let’s say we want to fire our “gun” with the space button. Then when you press the space button you want to run the shake method.
Now you can play the editor and adjust your noise to give you’re the exact shake that you want.