Shaking Your Virtual Cameras in Unity 2020

If we want to make our virtual cameras feel like a person is holding them or if we want the cameras to react to a force such as an explosion, the Noise properties is where we want to do this.

Noise Properties

“Use Noise properties in a Virtual Camera to simulate camera shake.

Cinemachine includes a Basic Multi Channel Perlin component, which adds Perlin noise to the movement of the Virtual Camera.

Perlin noise is a technique to compute random movement with a natural behavior.”

There isn’t too much to go over here, just play with the settings until you are happy with the feel.

If you are going for a person hand-hold shake, you’d probably want this to be on fairly consistent settings until the person is running or something than adjust the frequency and amplitude as needed during that phase.

For a shaky camera reacting to a force or explosion, you’ll want to either control through code the timing of these values ratcheting up.

Using Cinemachine Impulse System for Camera Shake Triggers

The Unity Cinemachine Impulse system is actually pretty cool!
There are all kinds of camera shake use-case scenarios that can take advantage of this system.

Need the camera to shake a certain way due to an explosion?
What about based on how close that explosion is?

How about the player ran into a wall and you want the camera to add “feeling” to that collision?
Did the player just get shot?
Is the player sprinting versus walking?

All of these conditions can be handled simultaneously because the impulse system lets us setup a setting profile for each one in advance.

We can then trigger the correct profile when the associated event happens.

“Cinemachine Impulse generates and manages camera shake in response to game events.

For example, you can use Impulse to make a Cinemachine Virtual Camera shake when one GameObject collides with another, or when something in your Scene explodes.

Impulse has three parts:

· Raw vibration signal: a vibration curve in up to 6 dimensions: X, Y, Z, pitch, roll, yaw.

· Impulse Source: a component that emits the raw vibration signal from a point in Scene space, and defines signal characteristics such as duration, intensity, and range.

· Impulse Listener: a Cinemachine extension that allows a Virtual Camera to “hear” an impulse, and react to it by shaking.

It’s useful to think about this in terms of individual “impulses.”

An impulse is a single occurrence of an Impulse Source emitting a raw vibration signal.

Collisions and events in your Scenes trigger impulses, Impulse Sources generate impulses, and Impulse Listeners react to impulses.”

Cinemachine Impulse Sources

“An Impulse Source is a component that emits a vibration signal from a point in Scene space.

Game events can cause an Impulse Source to emit a signal from the place where the event occurs.

The event triggers impulses, and the source generates impulses.

Virtual cameras with an Impulse Listener extension react to impulses by shaking.

In the image below[above], the figure’s feet are Impulse Sources.

When they collide with the floor (A) they generate impulses.

The camera is an Impulse Listener and reacts to the impulses by shaking (B), which shakes the resulting image in the game view ©.

Cinemachine ships with two types of Impulse Source component.

· Cinemachine Collision Impulse Source generates impulses in reaction to collisions and trigger zones.

https://docs.unity3d.com/Packages/com.unity.cinemachine@2.8/manual/CinemachineCollisionImpulseSource.html

· Cinemachine Impulse Source generates impulses in reaction to events other than collisions.

https://docs.unity3d.com/Packages/com.unity.cinemachine@2.8/manual/CinemachineImpulseSource.html

Your Scene can have as many Impulse Sources as you want.”

See the associated links for each Impulse Source Component above for more detailed information.

Key Impulse Source Properties

· Amplitude: controls the strength of the vibration.

https://docs.unity3d.com/Packages/com.unity.cinemachine@2.8/manual/CinemachineImpulseSourceOverview.html#Amplitude

· Orientation and direction: Impulse can transform the signal so that the vibrations are consistent with the direction of the impact that produces them.

https://docs.unity3d.com/Packages/com.unity.cinemachine@2.8/manual/CinemachineImpulseSourceOverview.html#Orientation

· Time envelope: controls the signal’s attack, sustain, and decay so that the signal fades in and out to the appropriate intensity and has a finite duration.

https://docs.unity3d.com/Packages/com.unity.cinemachine@2.8/manual/CinemachineImpulseSourceOverview.html#TimeEnvelope

· Spatial range: controls how far the signal travels in the Scene before it fades out completely

https://docs.unity3d.com/Packages/com.unity.cinemachine@2.8/manual/CinemachineImpulseSourceOverview.html#SpatialRange

Please see the associated links above for more detailed descriptions of each.

Cinemachine Impulse Listener

“Impulse signals and sources don’t do anything on their own.

An Impulse Listener is a Cinemachine extension that allows a virtual camera to “hear” impulse vibration signals and react to them.

When you add an Impulse Listener extension to a virtual camera, it makes the camera shake in response to the signals emitted from Impulse Sources.

In the simplest case, the Impulse Listener applies the signal verbatim to the camera’s Transform, causing it to shake.”

Adding Cinemachine Impulse System to Our Scene

I’m going to be using the same project from previous Cinemachine Virtual Camera articles.

I’ll make use of my existing 3rd Person Follow Camera that is attached to a Capsule game object representing a player character.

The first thing I want to do is add the Cinemachine Impulse Listener component to my 3rd person Virtual Camera.

Next, I need to create my Impulse Source game objects.

To keep these tidy, I’ll create an empty parent game object for them as well.
This will keep our hierarchy from getting out of control as we add more and more impulses to our game, if we were actually making a game.

Obviously I just have the 2 now, but habits have to be created!

We’ll add the Cinemachine Impulse Source components for each.

Now we create our Noise Settings profiles.

I’ve created two, one for on collision and another for a supposed explosion event near our player character.

I’ve set some values I hope will make sense, on a collision I don’t want the player to collide with a wall and the screen to react as if the world just ended.

Note next to amplitude for each value there is a check box and above it says “(non-random wave if checked)”.

Now, I personally like it to be a bit randomized, but if you want this camera shake to be exactly the same each time, you could check these boxes here.

I give some higher values for the explosion noise settings.

Now, our settings profiles won’t do anything until they are assigned to a live component on a live game object.

So, we do that as shown above.

Live and Learn — Proper Location of Collision Impulse Source

Since the Collision Impulse Source component requires a collider, it makes mor sense to add this component directly to my Target3 Capsule game object which has a collider and will be the source of the collision this will react to.

Remember to add the NoiseSettings-Collision to the Raw Signal field.

On my collision noise settings, I decided to remove positional movement of the camera and instead place the values on camera rotations.

These are the values I came up with.

I think these values worked out pretty good if I were emulating all of these colored rectangles being bushes or shrubs the player character is running through.

Now, essentially all of my customizations of this VCam noise is on the NoiseSettings-Collision object.
You could go further on the Target3 > Cinemachine Collision Impulse Source component itself which is pulling these noise settings.

Specifically randomize and Time envelope are pretty useful.

Additionally, I have not tested any of this, but the options for Direction/Mass/Speed and Spatial Range will really let you make these camera shakes more nuanced based on the source’s distance and mass.

This should allow the camera react to the difference between say an impact from walking into a wall and getting hit by a speeding car.

Now, I’m not going to go through all of this again with the Explosion Impulse Source, but it is very similar other than how it would be triggered.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why not just use ZoneMinder

Huobi Wallet event on ChainZ Arena

Penetration Testing : Getting started with Linux Commands Basic

Multiple provider use cases in Terraform

Actual state of verified Terraform providers

The Blender 2.8 Keyboard Shortcuts Cheat Sheet for Windows

The Blender 2.8 Keyboard Shortcuts Cheat Sheet for Windows

Serving multiple SSL certificates in your Go tests

How to Download a YouTube video in Google Colab

Set up a Volentix Node

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
GameDev Dustin

GameDev Dustin

More from Medium

Clear Shot Virtual Camera in Unity 2020

Adding Post-Processing To URP

Platformer: Wall Jump

Adding Fog to the HDRP Pipeline in Unity 2020