In this article I will show how I setup my scaling bar for my Thrusters Power Level in Unity.
I only want the thrusters speed boost to be able to be used until the scaling bar/meter reaches 0 and then it needs to recharge.
First I added a Slider to my hierarchy by right clicking and UI>Slider and I renamed it “Thrusters_Slider”.
I deleted the Background and Handle Slide Area child objects as they are not needed.
I positioned the Thrusters_Slider where I wanted it and set these settings…
Anchored it to the bottom left.
Fill Area should look like this…
…and Fill like this…
I also created some UI text with UI>Text and positioned it, changed font etc making it look like this…
Now it looks like this on screen before starting the game…
In my Player class I added a few different variables for the thrusters.
In my Start() method I added..
_canThrustersBeUsed = true;
In my OnThrusters method that gets info from my Thrusters action in my Controls InputActionAsset I changed a few things…
When the button/key for Thrusters is pressed it sets the speed to the boosted speed and the _thrustersAreBeingUsed bool to true.
When the key or button is no longer pressed it resets the speed and the _thrustersAreBeingUsed bool to false.
In my CalculateMovement() method it now checks if the thrusters are being used with that bool I set above in the OnThrusters method and decreases the _thrusterPowerLevel by Time.deltaTime multiplied by 3f so that it goes a little faster.
It sends the _thrusterPowerLevel through my reference to my UIManager singleton to the UpdateThrustersSlider method which updates the meter on screen.
If the _thrusterPowerLevel reaches 0 then it sends a bool of false to the UIManager’s ThrusterSliderColor method which changes the bar/meter to red.
It also sets the other bools to false and the power level to 0 and then calls the ResetSpeed() method
In the ResetSpeed() method in my Player class it checks if the _canThrustersBeUsed is false and if it is then it starts the ThrustersCoolDownRoutine(). It also resets the speed to the regular speed.
The ThrustersCoolDownRoutine() waits 1 second before recharging the meter, it works the same way as decreasing the power level value but without being multiplied by 3 so that it goes slower and of course incrementing with
+= I used a yield return null so that it waits 1 frame before continuing the while loop. Once the power value reaches 10 it also resets the color to blue with the UIManager and then sets the _canThrustersBeUsed bool back to true which stops the while loop and allows the thrusters to be used again.