Initiating A Camera Shake When The Player Takes Damage
One of the most important aspects of a game is making the player feel immersed in the game itself. Player Immersion is an ever-expanding topic with many arguments, but for today we are going to be addressing Camera Shake. This is a common tool utilized by designers to make the actions performed or witnessed by the player seem more impactful and/or more powerful.
A camera shake, like most other things, can be done in a variety of ways. Commonly though, it is done by simply moving the “transform.position.x” and “transform.position.y” of the camera object in short increments, at random directions, quickly, and over a small amount of time. The camera then resets back to its original position and done. After that, you just need to decide how you want it triggered.
This is what it looks like when you take damage without the screen shake.
This is what it looks like after adding a camera shake when the player takes damage.
Create a new C# script that includes the code seen in this example. Then attach this code to an empty game object. Make your player camera object a child of this object.
Then add this code to your player and make sure to cache the reference to the object that holds the “CameraShake” script.
And that’s it! You can replace the “.15f” float (duration) as seen in the method above in order to adjust the amount of time that the shake will continue, and you can replace the “.4f” float (magnitude) to change the intensity of the shake.