Implementing a cooldown system for the thrusters in my space shooter

Currently, the thrusters in my space shooter can be used indefinitely. There’s no fun in that. They need some sort of a cooldown system so the player can only use them for a short period of time, then they need to recharge. This is a somewhat complicated system, but I can handle it.

I created a few variables, one for the amount of boost remaining, one for the rate at which the boost will be drained, and two bools to use as checks for the current status. Most of the logic will be done in the movement method of the player script for this. When checking for input on the shift key, I added a few lines of logic. It will now drain the boost while the key is held down and sets the _boosting variable to true. On letting the key up, _boosting is set back to false and it starts the cooldown.

Player.cs

The cooldown is handled inside of a coroutine, since I want it to be time-based. Let’s look at that. After letting go of the shift-key, I want the boost to be unavailable for 1.5 seconds, then it will start recharging the boost level while the player is not boosting.

Player.cs

Now I just need to update a boost bar on the UI with the current level of boost. I found a healthbar asset that fits with my game and I saw a handy functionality in Unity’s library for UI Images. Given a number from 0–1, you can change what percentage of an image is rendered on the screen. In this example, I can use that to fill the boost bar relative to the _boostRemaining value. In the Image component in the inspector for the boost bar, I changed the Image Type to “Filled” and changed the Fill Method and Origin to be “Horizontal” and “Left” respectively. Now, I can modify the fill amount via code. I’ll do it in the UIManager, and have this method called from the player script.

UIManager.cs