Creating a Thruster Boost Feature for our 2D Galaxy Shooter in Unity-Part 01!
Objective: To create a Thruster Boost for our player in our 2D Galaxy Shooter in Unity!
Lets now give the player an opportunity to use a thruster boost to boost themselves out of harms way in case they find themselves in a tight situation with lasers and enemies trapping them. We also don’t want the player to abuse this feature so let’s make sure there is a limited supply that will eventually regenerate over time. Lets get started!
Adding the Speed Thruster
As we want the player to be able to choose when they want to use this and for how long, let’s give them some control by using Input.GetKey which detects when a key is held down. In our case this key is going to be the left shift.
We are going to create a new script which is going to cover this feature and also control the thruster gauge (which I will cover in another article). So let’s get started by creating a new script and adding some code:
Firstly, we are going to need access to our ‘Player’ script as this controls the players speed. So let’s create a handle to this and use GetComponentInParent in the Start() method. Don’t forget to null check, to make sure our scripts are communicating!
Within the Update() method, let’s start by detecting our players input of the left shift key. If this key is held down, then we want to call the ThrusterBoostActivated() method from the player’s script which is going to increase the players speed momentarily while this key is held down. Upon release of this key, we want the players speed to return to normal, so within our else conditional statement we are going to call the ThrusterBoostDeactivated() method. (Both of these methods are shown below, within the ‘Player’ script).
Included in these 2 methods, we are going to set the alpha colour of the thrusters SpriteRenderer component depending on whether it is being used or not. If ThrusterBoostActivated() is being called, set the alpha to maximum (1) and if ThrusterBoostDeactivated() is being called, set it to our normal cruising speed alpha (0.75).
For now we can add the ‘ThrusterController’ script to the player game object to see the changes!
Next Up: Part 02 is going to cover deepening the conditions used for our visual engine cues, including a smoking engine animation that occurs when the players thruster is overloaded from being used to much.
Useful Links:
https://docs.unity3d.com/ScriptReference/Input.GetKey.html
https://docs.unity3d.com/ScriptReference/GameObject.Find.html
https://docs.unity3d.com/ScriptReference/Component.GetComponentInParent.html