So I got my Bomb which is working totally fine.
In the UI I have an Image which is indicating on which time the player can place a bomb again.
The Image is filled at the before any use and when the player is placing one bomb the meter turns 0.
And now my problem comes in.
The fill amount stucks right after it turns 0. It doesn’t refill my Image complete.
I tried different things and was looking around but nothing helped.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BombUICooldown : MonoBehaviour
{
public float coolDown = 5;
bool currentCoolDown;
public Image bombImage;
void Start()
{
currentCoolDown = false;
bombImage.fillAmount = 1;
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Q))
{
currentCoolDown = true;
}
if (currentCoolDown)
{
bombImage.fillAmount -= 1 / coolDown * Time.deltaTime;
if (bombImage.fillAmount <= 1)
{
bombImage.fillAmount = 0;
currentCoolDown = false;
bombImage.fillAmount += 1 * coolDown * Time.deltaTime;
}
}
}
}
I think it is a conflict with the bool. Gosh, I already sitting to long on this.
Thank you for any help.
Two things to try:
-Print out the actual numbers so you are certain they’re what you expect.
-Use Mathf.Approximately() to compare floats. The previous thing might hint at why this helps.
I think the issue is line 35,
change
bombImage.fillAmount += 1 * coolDown * Time.deltaTime; // this says take the fillAmount (witch is currently 0, from line 33), then add 1 * 5 * 0.01ish, so it’s not 0, it’s just higher then 0, but not full
to
bombImage.fillAmount = 1; // this should make it full again
I don’t really understand your “refill”
Once you are <= 1, you set it to 0, set bool to false, and then you add a small amount to the fill. That’s it. You’re not incrementing back up. What is it you are trying to do once you get to 0?
@johne5 beat me to the point I was trying to make.
@Brathnann yah, I didn’t catch that part <= 1
so for 1 frame reduce the fillAmount, from line 29, then set it to 0. so maybe the total fill amount is higher then 1, but he has fillAmount = 1 in the Start()
So he might need to change the second if statement to <= 0.1f, and still use my edit for line 35
You can ignore the Start() function it’s working (with the issue).
Okay maybe I can describe it like that.
What I want it to do:
Image is filled at the Start
button is pressed
Image is empty (fill amount is 0)
Image is filling up again within the cooldown time
repeat on button press
everything works execpt the part with the fill up.
It currently goes down to 0 and is filling up to 0.08628.
When I press again it to 0 and back to 0.08628.
So we’re almost there, it only has to come back to 1.
here is what I was wanting you to try, and or explain
if (currentCoolDown)
{
//coolddown is active
// fillAmount is lessthat or equal to 1
bombImage.fillAmount -= 1 / coolDown * Time.deltaTime; //reduce fillAmount by 0.01f
if (bombImage.fillAmount <= 1) //check if fillAmount is lessthen or equal to 1
{
//true every frame
bombImage.fillAmount = 0; // set it to 0? why?
currentCoolDown = false; // we are at 0 so stop the cooldown
bombImage.fillAmount += 1 * coolDown * Time.deltaTime; // add a small amount to the fillAmount. why?
}
}
//-------------
if (currentCoolDown)
{
bombImage.fillAmount -= 1 / coolDown * Time.deltaTime;
if (bombImage.fillAmount <= 0.01f)
{
//fill is close to 0
bombImage.fillAmount = 0; //force fill to be 0
currentCoolDown = false; //stop the cooldown
bombImage.fillAmount = 1; //reset fillAmount to full
}
}