Simple camera shake in Unity

Daniel Kirwan
3 min readApr 23, 2021

--

A good way to show the player that they have taken damage, apart from the other elements I have in game like the hearts and damage on the player sprite is a camera shake. I use a free asset from the asset store called LeanTween. I would recommend this asset as it is great for creating simple animations as well, I used this for the game intro screen.

This is a lot better than using Unity animations as these tweens do not run every frame.

For the Camera shake I created a simple script that will move the camera on the x and then on the y. The amount you want to shake can be determined in the Unity editor. I created an empty game object in my game scene and added my new camera shake script.

The serialized field of Vector2 is for the amount of movement that you require and these can be adjusted in the editor because of the attribute of [SerializeField].

I then make a Transform for the object that is being moved. Which in this case is the Camera. Then, I am setting the initialPosition of the camera, so that when it is moved for the shake, it can go back to the original position.

The first method to start the shaking needs to be public as our player needs to be able to call the method when they are taking damage. This then starts a sequence of method calls which ends with the camera resetting its position.

The good thing with LeanTween is that you can chain multiple methods together. Once the HorizontalCameraShake() is called it moves the camera on the x-axis and then immediately calls the VerticalCameraShake(). This moves the camera on the y-axis and the DefaultCameraShake() method. This resets the position of the camera.

In the player we now need to setup the access to the CameraShake script.

Now we need to use the access we have and call the public method and start the camera shake when the player takes damage.

Now it’s time to go into the editor and add the movement numbers into the Vector2. Now we can test the camera shake.

You can see above the camera shake in action.

That’s it for this article. Be sure to come back and read my next article.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet