Previously, I’ve added a thruster to speed up movement by holding left shift and setting speed back to normal after releasing it. This time around, we will be implementing resource consumption and cooldown for the Thruster or it wouldn’t be fair. We will also allow the player to see how much thruster is being used and how much of it is being refilled.
First, I repurposed a Slider and make it act as a scaling bar:
This is the code to update the scaling bar. Notice that I used delegate so that the UI is only updated when thruster level has changed. You can also see from the code that the color indicator changes from nothing to red, yellow and green depending on thruster level.
Then on the Player, I had to update the Thruster code to calculate consumption. I used this formula to consume Thruster level:
_thrusterLevel = _thrusterLevel - _thrusterConsumeRate * Time.deltaTime;
On the other side, I used this formula to regenerate Thruster level:
_thrusterLevel = _thrusterLevel + _thrusterRecoveryRate * Time.deltaTime;
For balance, I made sure recovery is slower than consumption. Also, thruster recovery needs a cooldown period first:
if (((_lastThrust + _thrusterCooldownTime) <= Time.time) && _needsRefill)
{
//Refill Thruster level
}
Here’s the full code for the Thruster:
Now, I have a running thruster with cooldown.