Space Shooter Redux: Day 3
Today, we’re adding a HUD item to display the heat of our engine. As the player uses the thruster, the heat will increase until it reaches maximum and overheats. Then we disable the thruster until a short cooldown finishes.
Here are the variables we’ll need to manage engine temperature:
There’s no reason the temperature needs to be a range from 0 to 100. It’s just easy to think of it as a percentage, which will come in handy a bit later. The heat/cool per second values can be tweaked to your liking, but I found these to be pretty reasonable.
Now we need some logic to adjust the temperature according to the per second values:
These will be called during Update depending on whether the thruster is active or not. Here’s the logic for our HandleBoost function:
ActivateBoost and DeactivateBoost are just used to handle animations, so don’t worry about them for this. Now, if we serialize our _engineTemp field we can see the code working:
This is great and all, but nobody is going to be playing this game in the Unity editor. So I used the UI > Slider component to create a HUD indicator for engine temp. Its value is updated by the _engineTemp variable and it uses the Color.Lerp method to adjust between blue, orange, and red (specific shades I defined as CoolEngine, WarmEngine, and HotEngine respectively) as the temperature increases.
Finally, here’s the slider in action!