Phase 1 — Thrusters and Thrusters HUD

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.

Thrusters

The first challenge is a Thruster system, that increases speed momentarily when the Left Shift is held down.

Player script

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 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.

UI Manager script

Next we update the Player script with the code to use Thrusters only if it has fuel, and to refuel.

Player script

Here’s how it works

  1. In the block, when Left Shift is pressed, Player has fuel, and the canUseThrusters bool is true, then Player can zoom around.
  2. 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.
  3. 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.
  4. 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.
  5. If the Player doesn’t use up all the fuel, then the fuel is refilled in the method, at a slower rate.