2D Shooter: Boosting the Thrusters
Objective: Create a way where if the left shift button is held down, the player will gain a boost of speed for five seconds. If the thrust runs out of power, the player can’t thrust for another five seconds. While not using the thrust (when left shift button is lifted, it charges until full power.
I’ll say this right now, my charge is bit broken, and since I don’t have much time to work on it today, I’ll be taking another look to fix it later. In the meantime, I got everything else working how I like.
Let’s start with some pseudo code to figure out exactly what we need to do.
Okay, let’s go to Unity and make the thruster smaller on default, I changed the scale on all three axis to 0.5 so I could get this:
We’ll change it back to 1 when the thruster boost is engaged.
Back to the player script, I started by making a new method and an if statement of what I want done when the left shift button is held and a bool variable I made (_canThrust) is true. (It will be true on default). This is where we want to make the thruster big. As you can see, I also made a number of routines that I want to start when _chargingTime reaches certain values.
Before we make the routines, let’s take a look at what should happen when the left shift is released. We will want to reassign the speed of the player, and the scale and position of the thruster back to normal, and we’ll want to do this whenever the boost will not be active (like above when _chargingTime ==0).
Now I have a separate routine for when the thrust is charging and is overheated because I only want _canThrust to be false if _charging time reaches zero. If that happens, I don’t want the player to be able to boost the thrust again until five seconds have passed.
Here are the routines:
Now here is what I need to work out. For some reason, if the player releases the shift key before _chargingTime reaches zero, the routine for charging it back to 5 doesn’t play. As far as I can see, everything else has been working fine, and the issue as to why the ChargeRoutine isn’t working is not glaringly obvious to me. We’ll see if I can fix it tomorrow when I have more time.
For now, this is what it looks like: