Depending on what kind of energy you may be wanting in your game, you may do this in various ways. All in all, it’s actually quite simple to pull off.
To start, you will have to create either an int or a float variable within your script. This number value will represent the amount of energy left in your meter. For this example, I will be using a value of 100 so that it can be easily equatable to percentages (%).
This works for a system that drains your energy (_mana) by set amounts.
But what if you wanted it to drain incrementally over time?
This example uses a Coroutine to drain the player’s mana by 1 every .05 seconds while holding down the “Left Shift” button to cast a spell.
It’s that easy! You can use systems like this to create health mechanics, stamina, energy, jet fuel, etc. In the GIF below you, you can see the player ship begin moving at an increased speed as I hold down the Left Shift button and at the same time you see the fuel gauge in the upper left corner decrease.