Making Camera Shake when player is Hurt

Niraj Karki
3 min readMay 29, 2021

--

Day 37- Making 2D Space Shooter Game

Objective: Make the camera shake whenever the player gets hurt indicating a damage.

First create a camera shake script and place it in the Main camera. Then inside the camera shake script, declare a variable to store the default position, one to indicate player is shaking and lastly one to indicate player death.

Then store the default position in the declared variable for future use inside the Start method so that it stores the default position as soon as game starts.

Now create a Shaking routine which changes the camera position slightly to make the camera shake

Create a public method which will set the Shaking to true whenever called and start a coroutine that starts the shaking timer.

Now create a shaking timer routine that waits for 2 seconds and sets the shaking to false.

Also create a public Gameover method that sets the gameover variable to true to let the script know that player is dead.

Then in the update method, start the created shaking routine when the shaking is true and gameover is false meaning player is alive.

Also create a condition to set the camera position to default position whenever the shaking is false.

Lastly store the CameraShake script component inside the _cameraShae variable so that we can call the methods inside the Camera shake script.

Lastly, call the Shake whenever the player takes damage so that camera shakes.

And finally, call the GameOver method whenever the player lives is lower than one.

With this the Camera shake effect is successfully created.

--

--