Objective: When the Player takes damage the camera will shake for a split second.
The first thing that needs to be done is we need to create an Idle animation and a shake animation for the Main Camera:
Turn off Loop time for the shake animation and keep Loop time on for the Idle animation:
When the animations are created go to the Animator window. Create a trigger Parameter called Shake. Connect the Idle animation and the Camera Shake animation both ways. Set both the Transition Durations to 0 and add the Shake Parameter on the arrow to the camera shake.
Set Exit Time from camera shake to camera idle:
Once the animations are set up create a new C# script called Camera_Shake and add it to the Main Camera.
Open up the script and create a reference to the Main Camera and a SetTrigger to be called when the Player takes damage:
Don’t forget to drag in the Main Camera in the Unity Inspector.
Open up the Player script and create a reference to the new Camera_Shake script:
The last thing to do is call the CamShake Method when the player takes damage: