Thrusters & Scaling HUD Bar
Every gamer is familiar with some sprint & stamina mechanics. Today, I will be implementing something similar for the space shooter. A way for the player to speed up his ship for a limited amount of time, and an overheating punishment mechanic if the player uses up all of the “stamina.”
Create a limited speed boost mechanic, which, while the ‘SHIFT’ key is being pressed, speeds up the player and simultaneously will deplete the resource bar. Upon the bar depletion, lock this mechanic for a short amount of time.
- Check for the ‘SHIFT’ key being pressed in the ‘Update()’ method. When it is, run a speed-up method.
- While the ‘SHIFT’ key is being pressed, speed up the player and change the sprite for the thrusters.
- Upon releasing the ‘SHIFT’ key, return the player to his original speed and the original thruster sprites.
- Create a HUD on the UI, which will show the ‘Thruster’ speed up as a resource.
- When the speed-up is active, deplete this resource over time.
- When it is not active, replenish the resource until the maximum value.
- If the player uses up all of the resources, lock him out of using the thruster power until the bar is completely filled.
First up, in the ‘Player’ script, some variables I will be needing. Here I am mostly setting up the access to the HUD on the UI to show the player the amount of resources available.
I have decided to use a Coroutine for the speed-up. Here I am checking if the ‘SHIFT’ key is being pressed, and if it is, I am slowly depleting the resource bar and letting the UI element know about it.
Once the player stops pressing the ‘SHIFT’ key, or once the value of the resource is reduced to zero, I turn off all of the visualizations of the active thrusters and start up a second Coroutine.
The second Coroutine is responsible for filling up the HUD meter back up and updating it on the UI. Suppose the resources were depleted all the way down to zero. In that case, I am also locking the player from reusing the mechanic until the bar is completely filled - the player gets informed of this by an “Overheated” text being shown on the HUD.
And finally, I have created a ‘Thrusters’ script, which I have added to the ‘Thrusters_HUD’ UI GameObject; it is responsible for setting up the HUD at the start of the game and for changing the value on the UI:
And now, the Thrusters are working as I want them to! Of course, some adjustments need to be made to the code to make it prettier and mainly more optimized, but for now, this is what I am going with.
As you may have noticed, I have switched up the format of this article a bit. I will probably be keeping this format for the less informative and more objective-based articles.
But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!