Sign in to follow this  
Adam West

fps game rounds per second shooting

Recommended Posts

Adam West    219
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.

Share this post


Link to post
Share on other sites
Brother Bob    10344
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 this post


Link to post
Share on other sites
Tordin    625
[code]
m_cooldown += delta;
if(m_cooldown > m_rateofFire)
{
m_cooldown = 0.0f;
fire();
}
[/code]

think that would work.

Share this post


Link to post
Share on other sites
SimonForsman    7642
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 this post


Link to post
Share on other sites
web383    804
[quote name='Tordin' timestamp='1341486840' post='4955928']
[code]
m_cooldown += delta;
if(m_cooldown > m_rateofFire)
{
m_cooldown = 0.0f;
fire();
}
[/code]
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this