Game Development Stack Exchange is a query and reply website for skilled and unbiased recreation builders. It solely takes a minute to enroll.
Anybody can ask a query
Anybody can reply
The greatest solutions are voted up and rise to the highest
Asked
Viewed
4k occasions
I’m making a easy recreation interface. When fillAmount > 0
, the timer is stopped and reset. I need to restart the timer if the fill quantity doesn’t change for 3 seconds. All the code does now’s counting 3 seconds after which displaying the time when it stops. How do I repair it?
non-public float beginTime;
non-public float reStartTime;
public Barscript barscripts;
void Start() {
beginTime = Time.time
DefaultImg = GetComponent<Image>();
}
void Update() {
float t = Time.time - beginTime;
reStartTime = 3;
float t1 = reStartTime - beginTime;
string hours = ((int) t / 6).ToString();
string minutes = ((t % 6) * 10).ToString();
Debug.Log(barscripts.fillAmount);
if (batscripts.fillAmount > 0) {
DefaultImg.sprite = Emoji3;
beginTime += Time.deltatime;
timerText.textual content = " 0 : 00 "
Debug.Log(t1);
if (t1 < 0) {
timerText.textual content = hours + " : " + minutes;
}
}
}
Here’s a pattern picture of what I’m getting in the meanwhile.
$endgroup$
0
Assuming you need to create a time counter with deltaTime, it’s straightforward like this:
float timeRunning =0.0f;
Update()
{
timeRunning += Time.deltaTime;
if(timeRunning >= 3.0f)
{
//Do issues
timeRunning = 0.0f; //Restart counting
}
}
Inside the do issues you can verify the fillAmout’s worth with an older worth (it is best to create a lastFillAmount
variable)
$endgroup$
2
I feel this code ought to do what you need, its fairly self-explanatory however when you have any query simply do not hesitate to ask.
non-public float _lastFillAmountChangeTime;
non-public float _fillAmount;
non-public float _timer;
public float FillAmount
{
get { return _fillAmount;}
set
{
// register the change if enter worth != present worth
if(worth != _fillAmount)
{
_lastFillAmountChangeTime = UnityEngine.Time.time;
}
_fillAmount = worth;
}
}
non-public void Start()
{
// give these fields preliminary values
_lastFillAmountChangeTime = 0;
_fillAmount = 0;
_timer = 0;
}
non-public void Update ()
{
// replace timer
_timer += UnityEngine.Time.deltaTime;
// its greater than 3 sec from the final change of FillAmount
if (_lastFillAmountChangeTime + 3 < UnityEngine.Time.time)
{
// reset the timer
_timer = 0;
}
}
$endgroup$
You should log in to reply this query.
Not the reply you are searching for? Browse different questions tagged .
lang-cs