Introducing a Fuel System
In today’s article we’ll be improving the Player Speed Boost. We’ll implement a Fuel mechanism that only allows boosting if the Player has enough Fuel and in addition Fuel is consumed for as long as the Player is boosting. We shall also visually represent the Player Boost, and make new Power Ups for it.
Similar to how we created the Health & Shield Behaviour we have to create a few new Methods in the Player.cs:
To decrease the Fuel during boosting we can implement a Coroutine using a while loop. First in our current SetPlayerSpeed() method, we check if we’re boosting or not.
Then we can create a Coroutine, initialised by ConsumeFuel() in Update():
One could think; why not bind ConsumeFuel() to a key code? Well we could do that and it would all run fine except later, when we are to add fuel power ups we’d encounter a problem: Holding the key prior to picking up a fuel power up would end up in an everlasting boost until the Player lets loose of that key.
If we were to use Input.GetKeyDown (at key push down) it would work fine but then the coroutine won’t reactivate if we add fuel during holding that key because we only started it once before adding fuel. It’d result in always being able to boost for as long as the key is held because there is no fuel being consumed. Using Input.GetKey (during key push down) would also not be an option since it would run the coroutine in Update() every single frame, which is not what we want. Therefor we must use an exterior variable, independent from a key-bind. Optionally we could use Input.GetKey inside the coroutine itself.
Adding the Fuel Bar
Now we can create a new UI Element for it, this time I also added a text reference to display the fuel amount in %. For now this is fine since our max. fuel is 100, whenever we choose to adjust that we can come back and mathematically update the function.
Instead of using a horizontal bar, this time I use a circular background Image and a similar overlapping image with its Image Type set to Filled, using the Fill Method; Radial 360.
Fuel Power Ups
Now we can add the logic for the Fuel Power Ups:
Notice how this time when we Add Fuel, we do it slightly different. Say for example the Player has 97 fuel, and we pick up a fuel Power up adding 5 fuel, we’d end up with 102. In FuelBehaviour() we do set the current value back to the max.value when it surpasses it but there is another way we can prevent that. Before we add some fuel amount, we check if it is more than the max. fuel and calculate the remaining fuel. If it’s more, then add(but + → - in mathematics) the remaining fuel to(from) the fuel amount which on its turn we add to the current fuel.
What’s left for us to do now is to modify the PowerUp Enum, to update the Switch method responsible for activating different power up behaviours and to create three new prefabs we can add. In case you’ve been following along on this DevDiary chances are big that you are familiar with how to bring that to a good end.