fps game rounds per second shooting

Started by
3 comments, last by web383 11 years, 9 months ago
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,

thanks in advance.
Advertisement
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.

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


think that would work.
"There will be major features. none to be thought of yet"
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)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!


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();
}

This topic is closed to new replies.

Advertisement