working on a count down timer for a temporary boost in my game, where should i go next?

I am working on getting a count down timer to go so that when a certain boost or ability is picked the effect will last until the desired timer runs out and then go back to normal. Here is the code that I have so far, but it seems to tick down from 60 to like 58.something then just stop. so it ticks down once basically and freezes.

    public float SPF15BoostTimer = 60;
   
   
    public void SPF15SunScreenEffect()
    {
        if(GameController.control.sunScreenSPF15 >= 1)
        {
            SPF15BoostTimer -= Time.deltaTime;
            if(SPF15BoostTimer >= .5f)
            {
                GameController.control.shrimpPerTap = GameController.control.shrimpPerTap * 2;
            }
            else if(SPF15BoostTimer <= 0)
            {
                GameController.control.shrimpPerTap = GameController.control.shrimpPerTap;
                SPF15BoostTimer = 60;
            }
        }
    }
}

I have not worked with timers or anything like this for a power up or boost yet. Just wondering if someone could let me know if my script idea was on the right track or if i need to work this out another way. thanks!

first problem I can see in that code is that if you are calling this code every frame to update the timer you are doubling the “shrimpPerTap” each frame. In the else if, you are setting the “shrimpPerTap” to “shrimpPerTap”… which does nothing, I assume thats meant to be a “reset” of some sort?

“Just one tick and stops” implies that the function is only being called once. Where are you calling it from?

Well, like Lefty said, if you are calling this in update, which is probably needed to keep the timer running, then you are constantly doubling your “shrimpPerTap” every single update call. So, you’d need some type of statement telling it to not double it infinitely.

So you need to call this function in your Update and set an “if shrimpPerTap >= maxShrimpPerTap then shrimpPerTap = maxShrimpPerTap”. That really depends exactly what you are trying to do with that multiplier. In other words, set an extra variable for the max you want your multiplier to stop at if you are doing this the Update way.

P.S. You should be putting an “f” after those 60s :wink: 60f or 60.0f.

1 Like

i am calling it from a button click. I understand what i need to do is to divide by the amount i multiplied to reset it. That was a mistake I worked out. So instead of this should i have the timer part be set in the update function? and -= time.deltaTime is that the correct call to make it go down in seconds?

In that case make it a coroutine.

1 Like

Like BoredMormon said. Check coroutines. I recently finished tutorials, and decided to start doing things on my own by extending projects I made together with tutorials, and first thing I did were item packs that are dropped from enemies, which are giving boosts or putting heal over time on player for a set time, and I found coroutines to work great (though they looked scary at first!). They are easy to use once you start working with them. And there’s no need to play with any timers, since all you need to do is to tell coroutine to wait for the time you want before setting variables to values before change. It’s not perfect and there’s probably still better way to handle it, but it can show you how great coroutines can be!

    IEnumerator GetHealthPerSecond(int HealthGainAmount, float totalTimeToGainHealth, float hotTimeRate) {
        for(float i = 0f; i < totalTimeToGainHealth; i += hotTimeRate) { //For loop which increases i per hotTimeRate up to totalTimeToGainHealth (I think there's a high chance that there's better way to handle hot ticks - IMPROVE IT!)
            yield return new WaitForSeconds(hotTimeRate); //Wait before adding health for a hotTimeRate
            playerHealth.GetHealth(HealthGainAmount); //Call normal player GetHealth function each tick to give HealthGainAmount amount of health.       
       }
    }
1 Like

+1 for coroutines. They are extremely useful for similar types of things, but take a little getting used to, at first.

You would essentially be using a while loop in a coroutine for your timer. You would basically be doing a:

while(SPF15BoostTimer >= .5f)

… instead of using an if statement for it. If statements are only called once, hence why your current way would have to be called in the repeating Update set.

But, on the same note, you could do the same thing without a coroutine. The problem is, you generally can’t use while loops in regular updating, because it freezes everything else until the while loop completes. But, with a coroutine, the while loops essentially skip over themselves and move on with the rest of the game updating until the next round of updates.

A coroutine basically checks itself to see if the if/while conditionals are met, and if they are not, the system moves onto other tasks, instead of just hanging on a while statement, for instance. Then it rechecks them again on the next system update.

1 Like

thank you for showing me an example. I have only very briefly used coroutines thus far, but I can clearly see how it would be a very valuble next step in my programming knowledge to get a good handle on how they work. I will look at the unity documentation about them to start to understand them more.