2D Shooter: Shaking the Camera
Objective: Have the camera shake whenever the player gets hit.
First we’ll want to create the animation. While Main Camera is selected, create the animation and then create a new keyframe after moving the camera object to where you want it.
Now in the animation controller, we need to make an empty state as default, and create a transition to and from the shake. The transition going to the shake will need a trigger parameter so we can toggle it later through code.
Now in the player script, let’s make an Animator variable.
Now let’s grab the animator component from the main camera using GetComponent():
Now we’ll want to set the trigger whenever we want the animation to play. This will be in the damage method. I decided I want it to play no matter what kind of damage, so it can be placed directly in the method even before the code that detects the shield.
And we have a working camera shake!