Changing rate of fire for a gun

so im trying to decrease/increase the rate of fire for my gun. this is my code for whne the user shoots:



private AnalogListener analogListener = new AnalogListener() {



@Override

public void onAnalog(String name, float value, float tpf) {

if (name.equals(“Shoot”)) {

ammo = (int) (ammo - value * tpf);

if (ammo <= 0 && mag > 0) {

ammo += 30;

mag -= 1;

}

}

}

};



right now, the ammo goes down really fast. I have the fps limited at 60 fps, not that it matters, but i want the rate of fire to make sense with accordance with the framerate. I tried dividing the value but it didnt work. Any help is appreciated.

nevermind, i found a post and the guy had the same question as me.



What i did was:



float lastShotSince = 0; //make a float variable that basically records since the last shot fired (define it anywhere in the program)



in the update method:



lastShotSince += tpf; //where tpf is the variable in the paremeter for the update method that tells us how long since last time update method was called.



//depending on your gun, change the 0.1f to whatever number you like - the higher the number, the slower the gun shoots

if (name.equals(“Shoot”) && lastShotSince > 0.1f) {

//Call shoot method

lastShotSince = 0; //reset to 0, so we can shoot another bullet next time

}

1 Like

You are closer, but not quite there.



For automatic weapons in particular you will also need to cope with the scenario where your fps is lower than the guns rate of fire. I’d do it by having the “shoot” method having an integer to say how many bullets to fire (which can just be used to multiply the damage) and then as well as doing lastShotSince > rateOfFire do shoot((int)(lastShotSince/rateOfFire))

And I spotted another problem too, you are incrementing lastShotSince even when not shooting, and to be completely accurate you need to adjust lastShotSince as well to keep the remainder, so:



[java]



if (name.equals(“Shoot”)) {

accumulatedShot += tpf;

int shotCount = accumulatedShot / rateOfFire; // Rate of fire float seconds count

if (shotCount > 0) {

shoot(shotCount);

accumulatedShot -= shotCount * rateOfFire;

}

}

[/java]



That’s fine for high RoF weapons but will fall over for slow firing (think shotgun) in that if you press fire once then you will need to hold it down to make it fire again … so now you end up with:



[java]

// float accumulatedShot

// long lastShotTime



if (name.equals(“Shoot”)) {

accumulatedShot += tpf;

int shotCount = accumulatedShot / rateOfFire; // Rate of fire float seconds count

if (shotCount > 0 || lastShotTime < System.currentTimeMillis()-rateOfFire*1000) {

shoot(max(shotCount, 1));

accumulatedShot -= shotCount * rateOfFire;

}

}

[/java]



Although really to be more object oriented I’d delegate the fire logic to the gun itself and just detect that you need to shoot in the input manager…

wow, thanks for the tips zarch, i’ll definitely take the ideas you gave me into account when adding more gun code.