A Creating A Fire Rate

Ryan McCoach
2 min readJun 24, 2021
Shows the change of firing rate from slower to faster

This is a quick article on how to implement a fire rate to prevent the player for spamming the shoot button.

We are going to create two variables that help us control the time between shots. The first variable is does exactly that, it will be the time the player has to wait between shots. The second variable will track the last time the player fired. The Range attribute next to the _timeBetweenFire variable allows the Game Designer to use a slider in the Inspector to adjust the time.

Range Attribute (0.25f, 1.0f) in the Inspector

Lets see how we can use these variables. In the Update, we are going to check to see if the space bar was press and to see if Time.time is greater than _lastFireTime + _timeBetweenFire. First, Time.time measure the amount of time that has passed since the start, so 0 and incrementing up forever. When this running time is greater than the _lastFireTime + _timeBetweenFire. At the Start, _lastFireTime = 0 and _timeBetweenFire = 0.5.

Looking at this at the very Start, Time.time = 0 and _lastFireTime 0 + _timeBetweenFire 0.5.

Is 0 > 0.5? False. So we can fire until Time.time continues to tick and passes 0.5. Once it does, FireLaser is called.

In the FireLaser method, we just Instantiate the laster but important part is we need to set the _lastFireTime to the current running time (Time.time). If we press the shoot button 5 seconds into the game, the new _lastFireTime = 5 and now lets go back to the check…

Time.time > _lastFireTime +_timeBetweenFire

5 > 5 + .5 (5.5)

This means the player again needs to wait .5 of a second until running time gets passed 5.5 seconds to be able to call the FireLaser method again. This continues over and over.

--

--