Objective: Limit the Player’s thruster usage with a charge meter and have the charge regenerate after a small delay.
To begin we need to create a Slider that will act as the charge meter and have the color green with a white background. Also, we will use text to indicate the slider is for the thrusters:
After you create the slider, make it look how you want, and place it where you want on the Canvas. Open up the UI Manager script to create a reference to the slider:
Add the slider in through the Unity Inspector:
Now in the UI Manager script, we need two more variables that will allow us to indicate how full the slider bar will be:
We want these variables to be public because we want the Player script to have access to them.
In the Start method set the currentFuel = maxFuel so the bar will start full when the game starts:
The last thing to do in the UI Manager script is in the Update method, have the slider’s value = the currentFuel / mxFuel:
Now we Switch to the Player script and we will need four variables:
The first two determine how fast the Player will drain and refill the thrusters charge. The last two are for creating a cooldown before the charge will begin to refill.
First, we need to calculate how fast the charge will be used:
and now we can use CalculateFuelUse when the thrusters are active(When Left Shift is held down)
We can set _canRefillThrust to equal Time.time + _thrusterRefillColldown to stall the refill of the charge:
Finally, we can set our else to call a RefillFuel method to increase the charge until the currentFuel = maxFuel and call it in the else statement: