How to make RPM style fire rate?

Hello,

I was wondering how to make a RPM(Rounds per Miniute) style fire rate. I have a fire rate now, but the problem is that the faster the fire rate, the lower the value. So if you want a really fast shooting gun, ‘fireRate’ would be ‘0.1f’. If someone in the game reads a fire rate of ‘0.1’, they will think it is slow when in reality, it is the opposite of what they think. So please explain to me or show some code (or both) on how to implement a RPM style fire rate in my game. So basically, the higher the ‘fireRate’ variable, the faster the gun will shoot. Here is my code by the way:

if (gun.shootMode == Gun.ShootMode.Semi_Automatic)
          {
               if (Input.GetButtonDown("Fire1") && Time.time > nextShoot)
               {
                    gun.ammo.magazineAmmo--; //Takes away one bullet of ammo
                    nextShoot = Time.time + gun.fireRate;

                    Attack(); //Attacks
                }
            }
else
{
    //do other stuff I dont need to show you guys
}

Leave your code as it is and change what you display to the user
RPM can be calculated as 60/gun.fireRate

1 Like

This is in the update loop? You should really think about doing it in a coroutine instead with simple “start routine” and “stop routine” when the button is pressed and let go. That way, you can use “yield new WaitForSeconds(some float)” in a loop instead, which would make this far simpler. In that case, all you do is take the fireRate (let’s say 120 bullets per minute) and divide it by 60 (2 bullets per second) and then divide 1 by that number to get the number of seconds between each shot (.5, in this case). The yield is simply

while(true)
{
    return yield new WaitForSeconds(1 / (FireRate / 60));
    Attack();
}

or something.

1 Like
public float RPM;
private float fireDelay;

void Start()
{
fireDelay = RPM / 60.0f;
}

...
nextShoot = Time.time + fireDelay;
1 Like

Thank you man. I just added a “rpm” variable to my gun class, and now I can display the rpm from “gun.rpm”. Thank you!

My problem is now solved.

You are the man :slight_smile:

Please don’t necro old threads just to say thanks. This thread is 8 years old.

Simply use the like button to show your appreciation.

Thanks.

1 Like