Space Shooter Challenge: Thruster UI and Cooldown

Previously, I created a thruster that can boost the player’s speed when the shift key is held down. Currently, this can be held indefinitely, I wanted to create a system where the user has to think about when to use it sparingly.To do so, I would creat…


This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee

Previously, I created a thruster that can boost the player’s speed when the shift key is held down. Currently, this can be held indefinitely, I wanted to create a system where the user has to think about when to use it sparingly.

To do so, I would create a UI Slider to visualize the value of the thruster level. Then in code, I need to create a way to drain this value, but also a recharging function based on a timer.

To start, I created a UI Slider and deleted the Handle object, as I don’t want it to be user controlled. I appended some text above, and set the background to Red, and the Fill to Yellow.

To update the UI, I need to add the Slider to my UIManager. Then I can create a public method that sets the value based on a float variable passing in the time left.

In my Player Script, I needed some new variables. I created a private float to store the Thruster Level, a serialized private float to store and adjust the Cooldown time, and a private bool to check whether the Thruster is on or off for the while loops I will be creating.

I now needed to convert my original ThrusterOn and ThrusterOff methods to Coroutines. I started with just getting the Thruster Level decreasing for a start. My ThrusterCheck method now checks if Left Shift has been pressed, AND if the Thruster Level is above 0, if so, the Coroutine starts.

The ThrusterOn Coroutine contains the speed, visual, and audio functions it did before, but also sets the new bool to true. Then I jump into a while loop. I don’t want everything else to continuously update, hence just getting the initial press of the Left Shift key to start the Coroutine. Creating a while loop allows me to now check if the Left Shift Key is held, and also if the Thruster Level is above 0, as these will be our two exit functions for the loop.

In the loop, I can use yield return null so we can cycle through without any form of delay. Then I decrease the Thruster Level by Time.deltaTime, and simultaneously update the UI.

Now while the shift key is held, the Thruster UI Slider decreases, as our Thruster Level reaches 0. But the speed, visuals, and audio continue. After coming out of the while loop is where the ThrusterOff Coroutine can be called.

This Coroutine holds all the previous ThrusterOff functions, but also sets the bool to false.

In this Coroutine I want to wait for an amount of time, before adding Time.deltaTime to my Thruster Level. I only want this to run though, if the thruster on bool is set to false. Otherwise, if the Thruster alternates between being on and off, we end up with multiple calls for recharging the Thruster Level. Therefore I need an if statement, then I can wait for the Cooldown variable I set earlier, before finally creating a while loop.

In this loop, I want it to run only while the Thruster Level is below 5, so we don’t end up with more than 5 seconds use. It should also only run while the bool is false. So if the ThrusterOn Coroutine starts again, the recharging will stop. The contents of the while loop are simply the inverse of the ThrusterOn while loop.

Back in the intial if statement I want to confirm that the ThrusterOff Coroutine stops when the shift key is pressed, so I call StopCoroutine before starting the ThrusterOn Coroutine.


Space Shooter Challenge: Thruster UI and Cooldown was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee


Print Share Comment Cite Upload Translate Updates
APA

Calum Slee | Sciencx (2021-06-07T15:57:41+00:00) Space Shooter Challenge: Thruster UI and Cooldown. Retrieved from https://www.scien.cx/2021/06/07/space-shooter-challenge-thruster-ui-and-cooldown/

MLA
" » Space Shooter Challenge: Thruster UI and Cooldown." Calum Slee | Sciencx - Monday June 7, 2021, https://www.scien.cx/2021/06/07/space-shooter-challenge-thruster-ui-and-cooldown/
HARVARD
Calum Slee | Sciencx Monday June 7, 2021 » Space Shooter Challenge: Thruster UI and Cooldown., viewed ,<https://www.scien.cx/2021/06/07/space-shooter-challenge-thruster-ui-and-cooldown/>
VANCOUVER
Calum Slee | Sciencx - » Space Shooter Challenge: Thruster UI and Cooldown. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/07/space-shooter-challenge-thruster-ui-and-cooldown/
CHICAGO
" » Space Shooter Challenge: Thruster UI and Cooldown." Calum Slee | Sciencx - Accessed . https://www.scien.cx/2021/06/07/space-shooter-challenge-thruster-ui-and-cooldown/
IEEE
" » Space Shooter Challenge: Thruster UI and Cooldown." Calum Slee | Sciencx [Online]. Available: https://www.scien.cx/2021/06/07/space-shooter-challenge-thruster-ui-and-cooldown/. [Accessed: ]
rf:citation
» Space Shooter Challenge: Thruster UI and Cooldown | Calum Slee | Sciencx | https://www.scien.cx/2021/06/07/space-shooter-challenge-thruster-ui-and-cooldown/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.