In this article, we’re going to tackle adding a slight camera shake when the player takes damage. In Unity create two new animations that you will place on the ‘Main Camera’. One call it ‘Camera_Idle_anim’ and leave it alone for a moment. The second, name it ‘Camera_Shake_anim’ and then add a ‘Transform’ ‘Position’ property. Drag put your settings on the x at 0 and the y to 1. Then, drag the line to 1 second and place a marker. Set the x to 0.1 and y to 1.1. Add another marker at the 2-second frame and set the x to -.1 and y to .9. Drag the ending marker to the 4-second frame and set them the same as the first frame.

Next, we want to go back to the animations we just created. We want the ‘Loop Time’ active on the ‘Camera_Idle_anim’ and not on the shake.

Open up our Animator window and make the idle box our default. Set transitions going to the shake box and back to the idle box. We now want to create a trigger parameter and name it ‘Shake’. We will reference this in script later.

Click on the transition from idle to shake. We want ‘Has Exit Time’ to be disabled and the ‘Transition Duration’ at 0. We will also add the shake trigger to this transition.

On the transition from shake to idle, we want ‘Has Exit Time’ enabled and the ‘Exit Time’ at 1. The ‘Transition Duration’ needs to be 0 also.

Now, we want to create a new script named ‘Camera_Shake’ and place it on the ‘Main Camera’. In this script, we will add a serialized private ‘Animator’ and call it ‘_camAnim’. We will create a new public function called ‘CameraShake’ and just set the trigger of our ‘_camAnim’ to ‘Shake’.

We’re almost done. Now in the ‘Player’ script, we need to add a reference to the camera shake, so we need to create a private ‘Camera_Shake’ variable named ‘_shake’. We have to assign this in our ‘Start’ and null check like always. You will have to use GameObject.Find to complete the reference.

The final thing that will have to be done is calling the camera shake after taking damage. Look in your ‘Damage’ function for that and place it right after.

Now, when playing you will notice a slight jitter in the screen when you get hit. Adjust the camera shake animation if you want to change how it feels any.