Spawning after amount of time without spamming

Don’t use a Timer… that’s super overkill.

Instead, use just a float. I like to call my float “gunHeat.”

private float gunHeat;

private const float TimeBetweenShots = 0.25f;  // seconds

Now… in Update():

// cool the gun
if (gunHeat > 0)
{
  gunHeat -= Time.deltaTime;
}

// is the player asking to shoot?
if (PlayerAskedToShoot)
{
   // can we shoot yet?
   if (gunHeat <= 0)
   {
     // heat the gun up so we have to wait a bit before shooting again
     gunHeat = TimeBetweenShots;

     // DO THE SHOT HERE
   }
}

Way simpler. :slight_smile:

Now you can actually give the player powerups that reduce the TimeBetweenShots (which is a constant above, but you can make it variable).

Or you can have SUPER shots that take a really long time to recharge, for instance.

10 Likes