Keep the time left over after each shot: add delta to a timer and, while it holds at least one interval, subtract the interval and fire. Setting cooldown = 1.0 / rate after each shot rounds every shot up to whole frames. In our Godot 4.7 test a gun set to 10 shots a second and held for 10 seconds fired 75 times at 30 fps, 86 at 60 fps and 96 at 144 and 240 fps. At 20 a second it fired 150 at both 30 and 60 fps. The script fired exactly 100 and 200 at all five frame rates.
The code
Save it as fire_rate.gd. Make one per gun with const FireRate := preload("res://fire_rate.gd") and var gun = FireRate.new(10.0) and in _process() call gun.update(delta, Input.is_action_pressed("fire")). It returns how many shots to fire this frame, usually 0 or 1, more after a long frame. The first press fires at once, and letting go does not save up a burst.
extends RefCounted
## Fire at the rate you set at any frame rate: keep the time left over after each shot
## instead of resetting the cooldown, and fire twice in one frame when a frame is long.
var rate := 10.0 # shots per second
var _t := 0.0
var _was_held := false
func _init(shots_per_second: float = 10.0) -> void:
rate = shots_per_second
_t = 1.0 / rate # ready: the first press fires at once
## Call every frame. Returns how many shots to fire this frame (0, 1, or more on a long frame).
func update(delta: float, trigger_held: bool) -> int:
var interval := 1.0 / rate
if not trigger_held:
_t = minf(_t + delta, interval) # the cooldown runs, but no saved-up burst
_was_held = false
return 0
if _was_held:
_t += delta # no time is added on the press frame itself: that frame is the first shot
_was_held = true
var shots := 0
while _t >= interval - 0.000001: # the small margin stops float error from skipping a shot
_t -= interval
shots += 1
return shots
What we measured
We held the trigger for 10 seconds of game time with a fixed delta at 30, 60, 75, 144 and 240 fps and counted the shots. Two runs gave the same numbers.
- Resetting the cooldown after each shot, 10 a second, which should give 100: 75, 86, 94, 96 and 96.
- The same at 13 a second (should be 130): 100, 120, 125, 120, 127. At 15 (150): 150, 120, 150, 144, 142. At 20 (200): 150, 150, 188, 180, 185.
- The script: 100, 130, 150 and 200 at every frame rate.
- One frame of 0.25 seconds, like a hitch, during 20-a-second fire at 60 fps: the script fired 5 shots in that frame, after 20 in the first second, so the total stayed right. A gun that fires at most once per frame gets 1 there.
- Letting go for 2 seconds at 60 fps and 10 a second, then pressing again: no shots while released, one on the frame of the press, and the next 6 frames, 0.1 seconds, later.
Why it happens
With a reset, the cooldown counts down in whole frames, and the shot waits for the first frame where it reaches 0. Whatever that frame overshoots is thrown away, on every shot. At 30 fps 0.1 seconds should be exactly 3 frames, but 0.1 minus three steps of 1/30 is not 0 in floating point but a hair above it, 1.39e-17, so each shot waited a fourth frame: 300 frames, 75 shots. At 60 fps the same thing turned 6 frames into 7. Subtracting the interval keeps the overshoot for the next shot, so the average comes out exact, and the small margin in the script covers the float error.
What to change
- Fire as many shots as update() returns: a muzzle flash and a bullet for each. On a long frame they leave together, so if that looks bunched, move each extra bullet ahead along its path by the time it should already have flown.
- Fast bullets that pass through enemies: a raycast each frame. Ammo that goes missing on reload: magazine and reserve counts.
The flash on every shot: FPS Gun VFX for Godot 4 has 39 effects, among them 7 muzzle flashes that light the scene with a real OmniLight3D. Call play() on every shot: in rapid fire, smoke and shells still in the air are kept, because play() fires a copy instead of cutting them off.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: FPS Gun VFX for Godot 4 has 7 muzzle flashes with a real light, tracers and shell ejects that keep up with rapid fire.
More how-tos for FPS Gun VFX for Godot 4: Hit sparks from a pool of emitters
More how-tos: Projectiles and aiming
- A ball that hits the net however hard it is kicked
- A shot that meets a moving target
- An Area3D that does not see a bullet or another area
- Point swords, projectiles and models that face +Z at the target
- FPS shots that land on the crosshair
- A spell that keeps flying straight after it leaves the hand
- Turn towards a target the short way round
- A laser beam that stops where it hits, the moment you aim
- Fast projectiles that never fly through enemies
- A trajectory line that matches where the ball lands
- Aim a spell at the mouse on a 3D floor
- A homing missile that turns, but not instantly
- Particles fall sideways when you rotate the emitter
- Particles that fly into the player
- Gaps in the trail of a fast emitter
- Make a tower shoot the enemy closest to your base
- Hit the target with an arcing shot at any angle and height
- Kick a RigidBody3D ball with the same strength at any tick rate
- Reload without losing or making up rounds
- Fan out any number of projectiles evenly around the aim direction
- Recoil that settles back to where the player aims