How to get bool function true for x seconds?

Hello all! Im interested how i can make bool function true for only 3-4 sec? I want to make bool reload function, while reloading cant shoot

bool reloading = (Input.GetKeyDown(KeyCode.R));
if (reloading)
{
_canShoot = false;
}
but how I can back this bool to false so I can shoot again? :slight_smile:
Thanks in advance! :smiley:

Use coroutines.

You need stop checking if the reload key is down when its already down or you’ll get multiple reload routines running:

Something like this (There may be typos - typing directly):

public float reloadTime = 4f;
private bool reloading = false;

void Update()
{
    if(reloading==false &&  Input.GetKeyDown(KeyCode.R))
    {
        StartCoroutine(TriggerReload());
    }
}

IEnumerator TriggerReload()
{
    reloading = true;
    yield return new WaitForSeconds(reloadTime);
    reloading = false;
}

This way you can have clean code to deal with the reload thats grouped together. Just check the “reloading” bool when you need to.