Powerup for a certain amount of time

Hey, i’m trying to make a game in which you can use powerup’s, i managed to make them, which was pretty easy, but now I need to only make them last for example 5 seconds. Could anyone tell me how it would be possible to do that?

Thanks in advance !

1 Like

Hi @Nuubzz

Look into creating timers in Unity. There are many tutorials - just google it.

You could do something like this maybe:

void Update ()
{
    if (Input.GetKeyDown(KeyCode.P) && !powerupActive)
    {
        StartCoroutine(PowerUp(5f));
    }
}

IEnumerator PowerUp (float duration)
{
    Debug.Log("Power up started");
     
    powerupActive = true;
    yield return new WaitForSeconds(duration);
    powerupActive = false;

    Debug.Log("Power up ended");
}
1 Like

Oh thanks, i’ll look at that

@Nuubzz - I added an example to my post.

You don’t need to check every frame in the coroutine, you can just yield wait for the duration of the buff and then after that turn the bool off.

1 Like

Thanks, I did it you really helped ! :smile: