Galaxy Shooter — Adding Thrusters
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.
In this article, we’re going to add some “thrusters” to the player vehicle. The idea is that when the player is holding down the left shift key, the vehicle will move a bit faster. But only when the left shift key is down.
All of the work we need to do here will be in the script for the player object. The first thing we’ll need is a script level variable for how much our thrusters are going to increase the player’s speed by. This could be either a percentage we multiply by or a number we add to the base speed, depending on how we want to handle things. I went with the latter.
The rest of the code will be going into the Update method, as we will need to constantly monitor whether or not the shift key is down. A somewhat tricky aspect of this is recognizing the distinctions between the GetKeyDown and GetKey methods of the Unity Input class.
GetKeyDown returns true only once after the specified key is pressed. GetKey returns true for the entire time the specified key is being pressed. In other words, if we want an action to trigger once every time a specific key is pressed, then we want GetKeyDown. If we want something to happen continuously while a key is pressed, then we want GetKey. For our thrusters, we will want to use GetKey.
Let’s just step through this briefly. The first line creates a new float variable named adjustedSpeed and assigns it the value of the variable _speed. _speed is a script level variable that holds the default value of the player speed.
In the next line we have our conditional statement, checking to see if the specified key (left shift in this example) is being pressed. If it is, we go to our next line, which updates the value of adjustedSpeed by the value of our new script level variable _thrusterSpeedIncrease.
The adjustedSpeed variable is ultimately what we use to determine our speed, which will be used in the movement calculation portion of the code. That, however, is beyond the scope of this article. An earlier article of mine does address the topic of movement in Unity in an introductory fashion. Check it out if you need some help figuring that out.
Now if we run the game, we can see that the player ship does move faster when the left shift key is down, and slows down again when the key is released. Easy!
In the next article, we’re going to be taking a look at one of the existing powerups, the shield, and make some adjustments to it. Until then, good luck and happy coding!