How do I Update auto fire rate/speed when bonus item pickup is true?

Hello! I’m very new to game dev and here, so I apologize if this isn’t the correct place to ask this.

I’m trying to update the fireSpeed value in a variable like canFire = Time.time + fireSpeed so that the player auto fires at a higher rate when the bonus pickup is set to true. The issue I’m running into is that the Time.time > canFire appears to be stacking and kicks in after a few-second delay instead of instantly when the bonus pickup is true. Here is my code example below, thank you so much to anyone who can help! <3 I feel like I’m so close, yet so far away at the same time

   private float fireSpeed = 5.0f;
    private float canFire = 0;
 
    void Start()
    {
        gameManager = GameObject.Find("Game_Manager").GetComponent<GameBehavior>();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (gameManager.PowerUp)
        {
            fireSpeed = 1.0f;
        }
        else if (!gameManager.PowerUp)
        {
            fireSpeed = 5.0f;
        }
 
        if (Time.time > canFire)
        {
            Shoot();
        }
    }
 
    void Shoot()
    {
        canFire = Time.time + fireSpeed;
        GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    }

Hi!

You were quite close! Instead of forcing the fire speed directly into the canFire variable, I’d recommend just keeping track of last last time shot, that way if the fire rate changes, the calculation will adjust accordingly:

private float fireSpeed = 5.0f;
private float lastTimeFired = float.NegativeInfinity;

void Start()
{
    gameManager = GameObject.Find("Game_Manager").GetComponent<GameBehavior>();
}

        
void Update()
{
    if (gameManager.PowerUp)
    {
        fireSpeed = 1.0f;
    }
    else if (!gameManager.PowerUp)
    {
        fireSpeed = 5.0f;
    }

    if (Time.time > lastTimeFired + fireSpeed)
    {
        Shoot();
    }
}

void Shoot()
{
    lastTimeFired = Time.time;
    GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}