Visual Feedback — A Boost Bar
Many games feature a “stamina” system or something akin to that; the ability to speed the player up for a limited time by expending a resource. After some time, the player must wait to sprint again.
I previously implemented a simple stamina system for the space shooter example game, but without visual feedback, it is not clear what is going on to the player. Let’s fix that!
First, a look at the boost system:
In Start, I set the currentThrusters to = maxThrusters. This ensures players always start with the resource at its cap.
Next, a look at a new function, Thrusters:
While this function has more than a few statements, they are very simple:
The first “if” ”statement controls the function on when the player presses shift and has thruster available.
The second “if” ”statement turns off the player’s thrusters if they let go of the shift key, or run out of thruster.
The next two statements drain the thrusters, or refill them based on whether they are being actively used or not.
The two following “if” statements set the minimum and maximum values for the thruster system.
Finally, the last statement is part of the visual feedback we are wanting to build. This will set the current value of a slider to be equal to whatever the currentThruster float variable is.
What is the ThrusterSlider.value statement? Well we need a new UI slider and to save it as a variable in our script!
Position the slider where appropriate, then go back into your C# script.
The remaining work is done in the Unity window, as you set up the color and design of your slider. This is mainly going to be trial and error as you discover what you want your design to look like.
Let’s see it in action!