# fps game rounds per second shooting

This topic is 2293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey there, im having a brainfart with my shooting function in an fps game.

if i give a variable of rounds per second which will throttle the amount of times an if statement is run in a loop per second, how would i do this with delta time??

in english, i dont know how to implement a system where the gun will only fire e.g. 5 rounds per second,

##### Share on other sites
Keep track of the time the last round was fired. Once more than 1/5 of a second has passed since last round, fire a new round and increase the last fired-time by 1/5 of a second.

Note the last step: do not set the last fired-time to the curent time, but increase the last fired-time by the fire rate. That way, the fire rate will average out on the desired rate and you won't accumulate errors if you don't hit a time-sample at eaxctly the time a round should have been fired. If you hit a frame 0.05 seconds too late, you will have now have a 0.05 second head start until the next round should be fired to compensate for the 0.05 second the last round was fired too late.

##### Share on other sites
 m_cooldown += delta; if(m_cooldown > m_rateofFire) { m_cooldown = 0.0f; fire(); } 

think that would work.

##### Share on other sites
Also note that if you have a fixed timestep that is greater than the firerate you can have multiple rounds fire in the same logic update and for slow projectiles (i.e , anything that doesn't hit immediatly) you need to take that into account when setting their initial state)

##### Share on other sites

 m_cooldown += delta; if(m_cooldown > m_rateofFire) { m_cooldown = 0.0f; fire(); } 

Taking into account what Brother Bob stated, I would do this:
 m_cooldown += delta; if(m_cooldown >= m_rateofFire) { m_cooldown -= m_rateOfFire; fire(); } 

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009193
• ### Who's Online (See full list)

There are no registered users currently online

×