We do have some VFX for when our Player gets damaged — our Health counter decreases, and our Player Ship get’s damaged. But sometimes in the fray of battle it’s hard to notice.
In this article, we’ll make it very noticeable. By implementing a Camera Shake, the Player will definitely know that they have been damaged.
Creating a CameraShake script
For this implementation, we’ll create a new script, CameraShake, and attach it to our Main Camera.
All we will need is 3 variables to start:
[SerializeField] the variables so that you can tweak them through the Inspector to perfect the feel!
- _shake = will be our counter for counting down the shaking process
- _shakeAmount = is the magnitude of the camera shake
- _decreaseFactor = is the speed at which the shaking process will decrease
And in our Update():
This is the main logic that will shake the Main Camera until _shake is decreased back to zero, but we also need to create another function to set our _shake value to start the shaking process:
In the Player script
Now we can hook up the Camera Shake to be triggered by our Player.
First create a reference to the CameraShake script, and link the Main Camera to the field in the Player Inspector.
Now we can call MainCameraShake() from the CameraShake script:
Utilizing it when our Player gets damaged:
It’s little details like this that will really make your game shine in the Player’s eyes. Just be sure not to make the effect too dramatic and hinder the Player’s ability to play the game!
In the next article, we’ll be creating some new movement for our Enemies, making them more difficult to shoot and destroy!