Camera Shake when Damage- Game Dev Series 34

S.J. Jason Liu
3 min readMay 29, 2021

--

Objective: Camera shake instantly when player got damage.

Shaking camera might be the most immersivity way to make player feel the pain when got attacked by enemies.
To shake the camera randomly, I will use Random.insideUnitSphere as the random value.

This command will take a random point inside(or up)a sphere with default radius 1. We can use this command to run every frame with camera position as shaking.

Create a new script called CameraShaker and add it in Main Camera.

We will need some variables as the basic data.

  • _originPosition- to storage the original position of camera
  • _shakeRaduis- an adjustable value to control the shaking range

First we need to set the original position to our variable.

Then we can make it shake with random point.

This would happen if you press play.

We didn’t give it a statement to tell our camera shaker when to shake. Therefore we need another variable.

This is what we are going to receive a call from Player script.

Let’s create a new method.

This method will receive the int as the value of _shakeValue.

Then in Player script, create another line to call this method. As usual, create a variable and locate the component in Start().

Don’t forget the debug line.

Camera will shake at the point when our Player got a damage. This line should be under the lives count.

This would send the int 1 back to camera shaker when got a damage.

Back to CameraShaker, we will create an if statement with this value.

Press play to see the difference.

We successfully to make camera get shakes when got a damage. However, it won’t stop!
We need to create a decreasing value runs by time.

Create another variable in CameraShaker.

This value will control with time to decrease the _shakeValue. Let’s add some lines in the if statement.

When the camera start shaking, the _shakeValue will decrease by time multiply _shakeDecrease value till 0(or under). Then it should stop shaking.

Now, we can sit down and enjoy the result.

--

--

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.