2D Galaxy Shooter P1: Thruster HUD challenge complete!

I am so pumped! I struggled a bit in the beginning, but I finally found my way to finishing this challenge!

Yesterday, I posted about the thruster working without a HUD. This time, I have a HUD for the thruster and some logic to make it work.

The challenge was to make a UI element for the thruster and show that it scales when thrusting. There is also a requirement to use a cool-down system that I wrote about here.

I had the idea of having a small bar represent the thruster charge within the player game object. It is so the thruster bar can move along with the player at all times.

I used a UI Slider element for the bar. I deleted the Knob object and stayed with the background object of the slider.

I wanted the bar to be slightly below the player and I think it looks simple and neat!

In the UIManager script, I needed reference for the slider background image and a float for the fill amount. So I added:

[SerializeField] private Image _thrusterChargeImg = null;

private float _thrusterFill = 0f;

In the Player script, I added new variables:

The thrust charge will be the value that will change the visualization of the thruster bar. It needs to have a max charge as well that will restrict it from going over the max.

We then have the Deplete and Regen rates. It will allow us to make calculations with the thrust charge for when we use it. We need the thruster charge to deplete when the player is thrusting. We need the thruster charge to regenerate when the player is not thrusting.

The can thrust variable will give us the ok to thrust if it’s less than the time given. It will be checked in a condition when pressing the left shift key.

If pressing the left shift key, and the time is greater than the time given, and the thrust charge is greater than zero, then thrust my friends! Go with the wind!

Let’s take a look at the Thruster() function:

This little function gives the cool-down logic when to thrust and deplete the charge when doing so. Pretty neat, huh?

I also added a ThrusterRegeneration() function in the Update() function:

This function will regenerate our thruster charge when not thrusting.

And to make the thruster bar work in the UIManager script:

I divided the thrust charge by the thrust max charge properties in the player script to the thruster fill float variable. I then needed to assign the float variable to the “fill-amount” of the background image we referenced from the UI slider element earlier.

With all of this working together, we have a nice indication of the thrusters depleting and regenerating.

With this challenge complete, I can now move onto the next! It will be the implementation of a shield strength feature.

That is all for today! Thank you for your time!

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store