I don’t know why. So without the else there, my if statement works as intended. The special ability timer bar depletes right away after hitting Z and fills back up over 10 seconds. It’s only supposed to deplete when you hit Z while the character is touching ground.
My Else statement is supposed to be read if the character is not touching the ground, but instead, even though our ability is used the bar doesn’t deplete at all, it stays filled up.
public LayerMask whatIsGround;
public float maxTime = 10f;
float timeLeft;
private bool isGrounded = true;
void Update()
{
timeLeft = timeLeft <= maxTime ? timeLeft + Time.deltaTime : maxTime;
timerBar.fillAmount = timeLeft / maxTime;
if (isGrounded && timeLeft >= maxTime && Input.GetKeyDown(KeyCode.Z)) //greater than or equal to made the bar deplete which is what we wanted
{
timeLeft = 0; //After using our special ability, the bar increases over the course of 10 seconds. Once bar is filled up, we can use it again
}
//DON'T HAVE THE BAR DEPLETE IF WE AREN'T TOUCHING GROUND
else
{
Debug.Log("IsGrounded" here is overriding the if");
isGrounded = false;
}
}
}
You got a lotta conditions in there… I suggest untangling line 14 so that you test one condition at a time and can debug and reason about the consequences.
My thinking on line 14 is to unwind it to temporary variables:
bool deplete = true;
if (isGrounded)
{
deplete = false;
}
/// other stuff here, checking timers, jump key, etc
if (deplete)
{
// do depletion
}
The above is just sample logic, but generally if you only test ONE thing per if statement, you get in a lot less trouble.
Line 10 also made me blink… just use an if statement already!
if (timeLeft < maxTime)
{
timeLeft += Time.deltaTime;
}
if (timeLeft >= maxTime)
{
timeLeft = maxTime;
}
See how much clearer that is? Grandma could read it aloud to me over the phone and I would understand what was going on.
If that’s what it’s supposed to do, then your code is pretty far off from your intent.
Putting “isGrounded = false” inside the “else” doesn’t mean that it will run when isGrounded is false, it means that it sets isGrounded to false–which only matters if isGrounded was not already false.
Your “else” actually runs any time the “if” did NOT run–which means it runs if isGrounded is false OR if timeLeft < maxTime OR if the player did NOT press the Z button in the current frame. Which means darn near every single frame of your entire game.
Presumably that affects some other code that you haven’t shown here. (Generally, if your file is less than 100 lines long and you aren’t sure exactly what’s causing your problem, I’d suggest you post the whole thing.)
You’re also presenting us with bad information: Your code as posted won’t even compile (line 23), so whatever you’re testing with is different than what you’ve shown us. You should always copy/paste code directly from your tests when asking for debugging help, to make sure you don’t accidentally change something related to the problem. (If you want to simplify the example or hide some details, then create a simplified example, test it by actually running it, and then copy-paste from the test to the forum.)
Setting isGrounded to false every frame is a very weird thing to do. The name of the variable suggests that it ought to be true when you’re standing on the ground, but you’re setting it to false under almost any circumstances.
It’s impossible to know for sure without seeing your other code, but my guess is that you are setting isGrounded to true only when you first touch the ground–and then immediately setting it to false in your “else” clause–which means that your “if” never runs because “isGrounded” is never true. (Well, it’s true for approximately 1 frame when you touch ground, but if you don’t press Z in exactly that one frame then you won’t notice.)
I left out the start function, line 10 hooks up with whats in the start. The if statement also utilizes line 10, in which line 10 gets the ability bar to start filled up at the top when the level starts.
void Start()
{
timerBar = GetComponent<Image>();
timeLeft = maxTime; //without this line, the bar starts depleted and fills up over 10 seconds
}
Ah. When I copy+pasted the script, I wanted to change the debug line to that, so I edited the OP and changed the line to what you see in the thread post
Thanks. I did your recommendation for line 10, and it still did what it was supposed to do (bar starts filled).
For line 14, since you still kept timeLeft along with adding “depleted”, I feel like I need to change some things around. After doing more tests, I discovered that “timeLeft” determines how much the bar gets depleted. When "timeLeft=0" the bar depleted fully. When I tested “timeLeft=5”, the bar only depleted half ways.