Hey, so I am making a energyBar. And I until now I can fill and and unfill the bar over time using Time.time.
This works perfectly for the first try (when I press the key that executes energy draining).
But on the second time when I press the key it all screws up.
To clear things up: (barDisplay)value 1f is 100% of the bar. Meaning 0.1f is 10% of the bar.
So I subtract 0.25f over time to decrease the percentage over time. And add 0.1f to the bar to fill it up over time.
So it goes from 1f to 0f over time, and fills up from 0f to 1f over time just fine.
What does not work: When I press the key the second time, the “barDisplay” starts on -1f (meaning -100% of the bar)… It seems that it doesn’t reset it to 1f for the second time i press the key. Meaning I can’t go from 100% to 0% over time the second time the button is pressed.
Could you please help me out?
I’m new to coding and unity and i’m making my first game ever, so help is VERY much appreciated.(maybe theres another way to do it as well?)
Code (written in C#):
void Update () {
Debug.Log(barDisplay); //Log barDisplay in console
if(!timeBool && !CDbool){ //This is what I've tried... but doesn't reset it to 1.
barDisplay = 1f;
}
if(timeBool){
barDisplay = 1f - Time.time * 0.25f; //decreases from 1 to 0 over X seconds
}
if(CDbool)
{
barDisplay = Time.time * 0.1f; //fills up the bar again
}
}