Get that Camera Shakin’!

So far in Galaxy Shooter, the player can take damage if he runs into an enemy ship or an enemy laser. However, the effect is just very bland when it happens. Here the player takes damage twice by colliding with two enemy ships, and we get some smoking fire trail effects but not much else.

We really want the player to FEEL the impact of taking damage, and a great way to do that is to shake the camera.

The basic idea is that the camera itself should move around when the player takes damage. Of course, we don’t want the move the camera too much, since it’s supposed to be in a certain location so it can properly view the action that is unfolding in the game. However, we can temporarily slide it around in different directions to shake up the view of the game.

Basically we want something like this… except without having to do it manually

So how can we do this with code? First we’ll set up a new CameraManager script and attach it to the main camera in the scene.

We’ll want variables that control how long the screen will shake, how far the camera moves, and how long it takes for the camera to move around. We also want to keep track of the starting “base” position of the camera, so we always know what position of the camera is the default.

When the public method CameraShake is called, it will start a coroutine that will shake the camera over the next small period of time.

Now we need to figure out where the camera should go when it’s shaking around. We can write helper methods to do this. Both of these will return directions in the form of a Vector3, that points in a direction we want the camera to move to.

If the camera has already started shaking, we would pass in it’s last direction, so that it will next shake in a direction roughly opposite of that last direction. Otherwise it might keep shaking in the same direction over and over and produce an effect that isn’t very “chaotic”. Sometimes random effects need a helping hand to make it appear more random!

Finally, we have CameraShakeRoutine. Almost everything important is contained in a while loop that will stop when _totalShakingTime has elapsed, so that ensure the shake effect only lasts that long. During this time, we will get a direction to shake to, use that direction to find an actual position in world space that the camera will move to, and then move the camera to that position and back over a small period of time.

After each small camera movement, we wait for the next frame, update the timer variables, and then continue moving the camera and finding new shake directions as necessary.

Player.Start

Now the Player script can get access to the camera manager script and call the public CameraShake method in order to get things shaking!

As always, there’s room for improvement. It’s possible to adjust the camera shake so it becomes less intense over time, or have the intensity increase when the player is fully destroyed. Still, it’s a good idea to keep features as “good enough” and move onto more important things. We can always come back and polish later!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Blake Zoeckler

Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.