Simple Camera Shake when the Player Takes Damage!

Matej Marek
2 min readMay 10, 2021

Although we created a visualization of the damage for the player some time ago, it is still a bit lacking. Sometimes the player does not even notice that he got hit, and that is where a camera shake comes into play!

Objective:

Implement a “Camera Shake” system whenever the player gets damaged.

Implementation

First up, I have decided that the shake will be moving both the camera and the background image - for that reason, I have created a new empty GameObject, set both objects as children, and created a new ‘CameraShake’ script.

Once that is done, open up the script. The only thing we need here is to create a new public Coroutine, which will be responsible for that shaking. I have decided to make it a bit modular if I ever decided to add shaking for other sources than just the player getting damaged.

Here, we change the transform.position of the whole gameObject to random values between -1 and 1 multiplied by the magnitude for the specified duration. Once the shaking is done, we finally reset the position to the original one.

Now we need to make a few additions to the ‘Player’ script, where we define the two variables for the duration and magnitude and a variable that stores the reference to the ‘CameraShake’ GameObject - and of course, we will null check it.

Next, we only need to start the ‘Shake’ coroutine whenever the player gets damaged.

And that is it! The player will now get a mildly unpleasant shake whenever he gets damaged, which immediately informs him of the damage sustained - possibly making him want to dodge more so that the camera will not be shaking all that much!

But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!

--

--