Making Your World Shake in Unity.
Objective: let’s make the camera shake whenever the Player takes damage or gets destroyed.
While playing video games, graphics and sounds play an integral part in immersing the player into the game world you’ve created. Another important aspect is tactile feedback, like when your controller shakes when you get hit or destroyed. Obviously, we can’t make your keyboard shake, but we can make your lens into the world tremble by introducing a shake to the Main Camera. In this article, I will explain how I implemented this camera shake. It all begins with a little bit of code.
Create a new script, name it CameraShaker, and attach it to your Main Camera.
Open up this script in your editor and declare the following public method with the following two float variables: dur and mag.
Calling this method requires you to declare the desired values for dur (duration of the shake) and mag (magnitude of the tremor). An example of this would be:
Then, pass these values to the Shake coroutine like so:
This will take your dur and mag values, and store them into the duration and magnitude float variables of the Shake coroutine.
Your next step is to do a bit of housekeeping by storing the original transform position of the Main Camera into a Vector3 variable and resetting the new float variable called elapse to zero.
Then, run a while loop for however long it takes for the elapsed time (time since the method started the coroutine) to surpass the float value stored in duration.
Every time the loop increments, two new floats (x and y) are created by multiplying a random value between -1f and +1f by the magnitude. We then take these new values for x and y, and change the local position of the Main Camera by setting a new Vector3 (z remains equal to the Main Camera’s original position, so it never changes). This results in the Main Camera “looking” at the game world from slightly different positions in rapid succession.
Once the value of elapsed has surpassed duration, the while loop exits, and the local transform position of the Main Camera is reset to its original position.
This is what the entire script should look like:
Now we need to figure out when to call this method. It makes sense to generate a camera shake when the player takes on damage, so let’s head into the PlayerScript and add a few lines.
Begin by declaring a private variable of type CameraShaker and name it _camera. Then, in Start(), get a reference CameraShaker script in the Main Camera and store it in _camera, followed by a null check.
You then need to figure out where in your Damage() method (attached to your PlayerScript) to call the StartDamageCameraShake() method which is attached to your Main Camera in the CameraShaker script.
The visual result is as follows:
The nice thing about this setup is that as the game progresses, any event could call on the StartDamageCameraShake() method, and simply changing the duration and magnitude can provide a wide mix of visual results.
I hope you’ve enjoyed this article, and that it will help generate some ideas for your own game project. Thanks for reading! :)