Project 1: Space Shooter

How to shake the camera in Unity

New effect after the player takes damage

Eero Saarinen
2 min readJun 19, 2022
Camera shake in the action.

Even though I do have visual effects for the player taking damage, having a camera shake as well would increase the feeling of immersion.

For this to happen I decided to add a new script for my main camera, also the only one that I have, instead of adding this code to the player script. This way the handling of the shake is done by the main camera itself and the player only calls it when needed.

Public method to start the camera shake.

When the player takes damage it calls the public method PlayerDamageShake, which changes the boolean value of _isCameraShaking to true and sets and resets the duration of the shake.

The needed variables, their values and the logic when to shake the camera.

The Update method on the other hand checks if this boolean value is true, and if so calls the ShakeCamera method that does the actual shaking. Once the shake duration has ended the boolean is changed back to false and the shaking stops, with the camera position returned to the original position.

The method to shake the camera.

The shake itself is just changing the camera’s position with the help of the method Random.insideUnitSphere. Since it can have values on the unit sphere, I have to decrease this by multiplying the resulting point in the unit sphere or its surface with a magnitude value of my choosing. This is to prevent the shake being so extreme that it makes the game unplayable.

The extreme shake if the random unit sphere point is not adjusted.

--

--