By this point a basic game is done, and all these subsequent posts are challengers to test our coding knowledge and explore new tools to implement features. I’ll write about some interesting highlights.
The first challenge is a Thruster system, that increases speed momentarily when the Left Shift is held down.
The second related challenge is to give the Thrusters a visual component as well as a cooldown.
To create a visual, create an Image under the Canvas, I called it “Fuel_bar”. In the Inspector, anchor it to the left but ALSO hold Shift to set the pivot. This is important.
By setting the pivot, it allows the image to shrink towards the left the way it does in the first image of this article.
Now we need to add this Image to the UI Manager and have code ready to update it. In the UpdateFuel method, it’s constantly being passed a float of remaining fuel, and the Image will scale along the X-axis according to the ratio of remaining fuel/total fuel.
Next we update the Player script with the code to use Thrusters only if it has fuel, and to refuel.
Here’s how it works
- In the CalculateMovement block, when Left Shift is pressed, Player has fuel, and the canUseThrusters bool is true, then Player can zoom around.
- Fuel will be consumed at a rate of 0.5f per frame, and that value is sent to the UI Manager to be updated on screen.
- If the player uses up ALL their fuel, then it sets the canUseThrusters bool false, and starts the refueling coroutine. The Player cannot use thrusters while this coroutine is active.
- The refueling coroutine refills fuel at a fast rate, after a three second delay. When the Player has a full tank, then it sets canUseThrusters back to true.
- If the Player doesn’t use up all the fuel, then the fuel is refilled in the Update method, at a slower rate.