How can you limit the number of shots per second?

I’ve looked through the Time and Mathf.Clamp docs, but I don’t understand how to set this up.

I am trying to limit the number of shots that can be fired per every 3 seconds to 3. If the amount of shots equals 3 within the same 3 seconds, a boolean variable will become true which will delay the next ‘set’ of shots using the yield waitForSeconds() function, then the boolean will switch back to false once again.

Any recommendations?

I’m no expert (I’m here trying to answer a similar question that I have) but it would seem to me that both these answers do not prevent in excess of 3 shots in 3 seconds, technically the 1st answer does but very badly, and the second answer doesn’t at all.

I think the first answer is saying after the 3rd shot there’s a 3 second reset timer, which means if I fire 2 rounds and then wait 5 seconds before firing a 3rd I still have to then wait 3 more seconds to fire another - doesn’t work for me.

The 2nd answer suggests that If I fire a shot, and then 2.9 seconds later fire another 2 (taking 0.1 seconds to double click), I can then fire a further 3 shot immediately. Meaning I fired 5 shots as quickly as I can click.

I guess the solution I’m looking for is that every shot adds to a counter and in the same frame set a timer that will deduct that shot from the counter 3 seconds later. Obviously it can’t yield and prevent the script from running for those 3 seconds else I’d only get to fire 1 shot in 3 seconds.

I am a complete noob, maybe I just shredded someones answer for no reason. The answer I was looking for is fop the release of enemies in a wave without having them on an exact time schedule or equal intervals.

EDIT…

After realising I extended the question in the answer box here’s at least a pseudo answer not sure if the “yield” will pause the whole script though, I seem to remember something about Ienumerator but not sure if you can do that in JS…

var counter: int = 0;
var shotsPerTimedInterval: int = 3;
var timedInterval: int = 3;
 
function Update(){
	if(Input.GetKey("FireKey") && counter < shotsPerTimedInterval){

	//Fire Shot Code

	DeductShot();
	}
}


function DeductShot(){
	yield WaitForSeconds(timedInterval);
	counter--;
}

Here is what I used in my game. The shootRate means how many shots I can fire per second. Eg So if it’s 1, one shot can be fired and then wait until one second has passed. If it’s 10, then I can fire as many as 10 shots until one second passes.

Here is the code:

//Shooting rate
if(shootTimer >= 0)
    	shootTimer -= Time.deltaTime * shootRate;
    
//Shoot
if(ammo > 0 && shootTimer <= 0)
{
    	if(Input.GetButtonDown("Shoot"))
    	{
    		shootTimer = 1;   //This is the trick right here, setting the timer to 1 (1 second)
    		Instantiate(Action.projectile, Action.shootPosition.position, Action.shootPosition.rotation);
    		ammo -= 1;
    	}
}

You need to set it up like this or similar:

var counter: int = 0;
var limit: boolean = false;

function Update(){
if(counter == 3){
limit = true;
Reset();
}

if(Input.GetKey("z")&& !limit){
FireShot();
counter++
	}

}

function Reset(){
yield WaitForSeconds(3);
counter = 0;
limit = false;
}

float resetTimer = Time.time + 3;
int shotsFired = 0;

void Update() {
   // reset shot count?
   if (Time.time > resetTimer) {
      shotsFired = 0;
      resetTimer = Time.time + 3;
    }

    // shooting?
    if ((playerWantsShot == true) && (shotsFired < 3)) {
       // fire shot
       shotsFired++;
    }

}