Galaxy Shooter — Shaking Things Up

Joshua Nielsen
4 min readAug 4, 2021

Greetings, and welcome to this special series of articles. In this series, the intention is to take a solid functional base game and add some extra features that (hopefully) make it just a little more special.

The base game we have here is called Galaxy Shooter. It’s a Galaga-style 2D shmup that I put together while going through the 2D Game Development course for GameDevHQ. It has a controllable player ship, enemies, powerups, sound effects, music, and more. It is, as stated above, a fully functional game that can already be played and enjoyed in its current state.

But let’s see what else we can make it do.

Presently, we have a few ways in which the player can determine their ship’s status. If the player has a shield, they can see its opacity increasing and decreasing as the player gains the shield and then loses it through damage. The player’s ship also shows outward signs when the player has taken damage. Finally, the UI displays very clearly in the corner of the game just how much damage the player has taken and how much more they can take.

However, they don’t really get much feedback in the moment. If the game is getting chaotic, they may not have their eyes on either their own ship or the UI. Wouldn’t it be nice if we could make it very clear to the player that they are taking damage? Maybe give them a little shake?

That’s exactly what we’re going to do next. Shaking the camera/screen is a tried and true signal to the player that they are taking damage. So let’s get started!

How are we going to do this?

So we want to see the screen “shake”. But what does that mean exactly?

To answer that question, we need to consider just how we see anything in Unity. Well, it’s through the camera, right? Every scene has a camera and the player views the scene through the camera.

The camera is also a game object, like everything else in the scene. And also like everything else it has a transform, and that transform can be adjusted.

In this particular game, the camera hasn’t needed to move thus far, but movement is how we’re going to accomplish our “shaking”. Several small, random movements of the camera over a short period of time will create the illusion that we seek. And it’s all going to be done through scripting.

Scripting the camera

To start, we will need to find the Main Camera in our scene and add a new script for it. So we have a default script now for our camera, which means we have a Start and Update method in place. Is that what we need though? Let’s think.

The Start method runs once when the game object is created. Our camera is created when the scene is created. The Update method runs constantly for as long as the object exists.

What we want is for the camera to shake only when the player takes damage, so neither of these methods seem quite appropriate. What we will want is a new method that is called from the player script whenever the player takes damage.

We have two requirements for this new method. The first is that it needs to be a public method, so that the player script can run it. The other requirement is that we know that we want the camera shake to be an effect that runs over a brief period of time, which means we’ll want a coroutine.

So the idea with this coroutine will be to do the following: First, take note of the camera’s normal position. Then, over a specified period of time, constantly shift the camera’s position around where it normally would be. Once that is finished, return the camera to its normal position.

Camera shake coroutine

The two parameters we have here are the two aspects of this feature that can vary depending on how we want the camera shake to look and feel. We could hard code these values here, but we may want the camera shake to act differently in different situations, so better to set them as parameters.

The duration parameter determines how long the camera should shake, and the magnitude parameter determines by how much the camera shakes.

Once we have the camera script finished, we finish our work in the player script. First we’ll need a reference to the camera script. We can get that in the Start method by using the Find method of the GameObject class and looking for the camera object. Once we have that reference, all we need to do is start our new camera shake coroutine whenever the player takes damage.

Calling the camera shake coroutine from the player script

And with that done, here is the result:

Camera shake!

Great! Now the player definitely can’t miss when they’re taking damage.

That concludes our work on the camera shake feature. In the next article, we’re going to give the bad guys some attention. Until then, good luck and happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response