We can’t have unlimited power.

Chance Cornell
4 min readJul 30, 2021

This article, we will tackle adding a slider meter for our thrusters so the player can’t continually use them throughout the entire game. We will also create a cool-down system for the thrusters. There is also a bug with the speed powerup that we currently have. We will address that issue as well and come up with a solution that keeps everything working.

First, in our hierarchy, we want to create a new UI slider as a child of our ‘Canvas’ object. We will name it ‘Thruster_Charge_Slider’. We will also create a new UI Text object as a child of the slider. Just name that ‘Thruster_Fuel_Text’. Design the thruster meter however you want. I made mine with a white background and a green filling.

Open up your ‘UIManager’ script and we will now work on getting the mechanics of the slider to work. We will be adding some new variables. First will be a serialized private Slider named ‘_thrusterSlider’. Make sure to add the slider UI object we just created in this field in Unity. Next, we will create two public floats. The first is called ‘maxFuel’ set to 100f, and the second is named ‘currentFuel’.

In ‘Start’ we will set ‘currentFuel’ equal to ‘maxFuel’.

Lastly, in ‘Update’ we will set the value of ‘_thrusterSlider’ equal to ‘currentFuel’ divided by ‘maxFuel’.

We’re now going to open the ‘Player’ script and add some variables here, all of which will be private floats. We will want to serialize one called ‘_fuelBurnRate’ and set it to 30f. We will serialize another called ‘_fuelRefillRate’ and set it to 20f. We will also serialize one called ‘_thrusterRefillCoolDown’ and set it to 2f. The final float won’t need to be serialized. We will just call it ‘_canRefillThrust’.

We will now create a new private function called ‘CalculateFuelUse’. Inside we will set the ‘_currentFuel’ variable of the UI manager to subtract our ‘_fuelBurnRate’ multiplied by Time.deltaTime.

Next, we will make another public function called ‘RefillFuel’. We’re going to write an if statement that says if ‘_currentFuel’ is less than ‘_maxFuel’ AND Time.time is greater than ‘_canRefillThrust’, then we will add ‘_currentFuel’ to ‘_fuelRefillRate’ multiplied by Time.deltaTime. Else if, ‘_currentFuel’ is greater than ‘_maxFuel’, then ‘_currentFuel is equal to ‘_maxFuel’. There’s a lot to that, but look at the code snippet and read along with this to fully understand what’s happening.

This is where we’re going to fix up our bug with the speed powerup. We actually won’t need to touch the code for the powerup at all. The issue was with how the thrusters were originally written.

We’re going to create a new function called ‘Thrusters’ and put all our thruster logic from the previous article in there. We will be changing the starting input to have the get left shift key AND if the ‘_currentFuel’ of the UI manager is greater than 0. Then inside that statement, we will call our ‘CalculateFuelUse’ function then set ‘_currentSpeed equal to ‘_thrusterSpeed’. Then we will add to our else if statement, saying that ‘_currentSpeed’ is equal to ‘_speed’, then our ‘_canRefillThrust’ is equal to Time.time plus our ‘_thrusterRefillCoolDown’ variable. We’re adding one final else if statement to this for the powerup, saying that if ‘_isSpeedBoostActive’ is equal to false, then we set our ‘_currentSpeed’ equal to ‘_speed’. Then we will ‘RefillFuel’.

We will then call the ‘Thrusters’ function at the end of our ‘CalculateMovement’ function since it is being run in ‘Update’. This will make that function ran in the update as well.

Hit play and test out your thruster meter.

--

--