Day 41: Thrust HUD with Cool Down
Objective: Give player feedback on temperature of thrusters!
We don’t want to get the player the ability to use the thrusters the whole time they play, right? In this game we’ll limit the use of the thrusters by simulating overheating.
We’ll start with the variables we’ll need. We need the max that the temp can go before over heating, the current temp of the thrusters/engines, and a bool to know if the engines is overheated or not.
The variables are done now to increase the current temp while the left shift is being held. When we hold down the left shift we will want to make sure that the engine isn’t overheated.
While the left shift is being help, we’ll increase the currentEngineTemp by time.delta. We’ll also check to see if the currentEngineTemp is greater or equal to the maxTemp. If it is we’ll set isEngineOverHeated to true so we can’t use the thrusters anymore. We’ll also start the coroutine to cool down the engines.
In the Coroutine we’ll take away from the current temp by a certain amount every second. Once it is zero or less, it’ll assign isEngineOverHeated to false and break out of the while loop and allow for use of the thrusters again.
Next will be to connect the UI. For this I created a doughnut and turned it white.
We’ll import this into Unity and under the canvas, create a image object. In the image object under sprite renderer add this in the source image.
In the UI_manager script we will need to get a reference to this object.
Now we’ll create a public function to get the currentEngineTemp and a bool to see if the engine is overheated.
To get the image to show any amount we’ll equal the _heatGauge.fillAmount = currentTemp.
On the Image Object we created for the gauge there will be a image type. We’ll set that to filled. Method will be radial 360. Origin is where it will start filling from, I selected left but you can do right up or bottom. You can also select if you want it to fill clockwise or counter clockwise.
Next we will check to see if the engine is over heated. If it is turn the color red and keep it that way until it’s cooled down. If it is not, it’ll lerp, slowly change, from green to red based on the current temp.
Now that the UI logic is filled out we’ll move back to the player script. Under the section we check for input, we’ll call the public function from the UI and add the correct variables that are needed.
In the function, when we pass in the currentEngineTemp, we need to divide by the max temp. This will increase the fill amount of the image based on the percentage of the currentEngineTemp.
That’s all it takes to give player good visual feedback on engine temp to overheating.