Shell Shock: Camera Effect on Damage (Game Dev day 39)
Objective: Provide a subtle camera shake when the player takes damage.
Currently there are two forms of feedback to alert the player that they have taken damage; the Lives UI updates and a visual effect on the Player’s controllable ship as well.
To make sure the player is aware that they have been damaged, I added a camera shake when the damage function plays.
I created a new script called CameraShake and attached it to the Main Camera Gameobject.
Next, I wrote out the logic to rapidly move the main camera in a random direction on both the X and Y axes. As this is a 2D game, I left the Z axis as is. this coroutine is set to public so it can be referenced from the Player script later.
Next, to ensure that the camera returned to its proper location after the Camera shake I created an Empty GameObject called the Camera Anchor. I made sure it had the exact same position in the game as the Main Camera, and then I made the Main Camera a child of the Camera Anchor.
All that’s left is to call this coroutine during the Damage function on the Player script.