Continuous firing - Can we limit the rate of repeating firing as well?


I got the idea of the continuous firing when we hold the space bar. However, by pressing repeatedly the space bar very fast, you can sometimes get a faster rate of firing, which is a problem I believe. What would be the best way to limit that?
I have thought of two ideas but none is intellectually satisfactory :slight_smile:

  1. Limit the number of projectiles present in the game at any time. Not sure what the code would be but it should be feasible by counting objects of type projectile and using an condition in the Fire() method. Not really satisfactory because it’s a bit arbitrary and difficult to set since it will depend on the distance of your shredder and the speed of your projectiles.
  2. Using a coroutine? Not too sure about that one but it seems it is possible to run it in parallel, make it wait and at the end of it to set a bool to true, so that when we press the space bar, it will fire only if the coroutine is done executing (therefore only after a certain time).

I’m sure there must be a smarter way to solve it. Any idea anyone???

1 Like

Hi, I encounted the Space Bar spam issue :slight_smile: that’s my soulution:


Smart :slight_smile:


For future projects, if there’s something that has a long cooldown period (say, a weapon or ability you can only use every thirty seconds), it might be a good idea to set fireCounter to equal fireRate on Start(). That would make sure it would be immediately available to use instead of counting up from the start of the game or level.

I encountered this same issue. I have kids who “playtest” my games. I do not want them hammering the living heck out of my keyboard so I wrote some limiting code. This is based around the code found in the Unity space shooter tutorial.

Shoot is called from Update. nextFire is a private float. fireRate is a public float so that it can be set from the inspector. The other vars are setup pretty much the same as in the lecture.

It’s worth noting that I also put my shot movement code into a script on the shot object itself so you don’t see that here and would need to add it in to get the shots to move forward.

1 Like

Privacy & Terms