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:
[SerializeField] private float _thrustCharge = 0f;
[SerializeField] private float _thrustMaxCharge = 20f;
[SerializeField] private float _thrusterDepleteRate = 0.05f;
[SerializeField] private float _thrusterRegenRate = 0.05f;private bool _isThrusting = false;
private float _canThrust = -1f;
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 (Input.GetKey(KeyCode.LeftShift) && Time.time > _canThrust && _thrustCharge > 0)
{
_isThrusting = true;
Thruster();
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
_isThrusting = false;
}
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:
private void Thruster()
{
_canThrust = Time.time + _thrustDelay;
ThrusterCharge -= _thrusterDepleteRate;
}
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:
private void ThrusterRegeneration()
{
if (!_isThrusting && _thrustCharge < _thrustMaxCharge)
{
_thrustCharge += _thrusterRegenRate;
}
}
This function will regenerate our thruster charge when not thrusting.
And to make the thruster bar work in the UIManager script:
_thrusterFill = _player.ThrusterCharge / _player.ThrusterMaxCharge;
_thrusterChargeImg.fillAmount = _thrusterFill;
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!