Recoil that rotates the camera and then lerps it back mixes the kick with the player's own aim, so the settle-back either pulls the view to the center or undoes the mouse. In our Godot 4.7 test the player aimed 10° up, fired 10 shots that kicked 1.5° each, and pulled the mouse down 0.4° per shot to stay on target, so they meant to end at 6°. Lerping the camera back to 0 ended at 0.00°, and lerping back to the pitch at the first shot ended at 10.00°. Keeping the kick as its own offset on top of the aim ended at 6.00° at 30, 60, 144 and 240 fps.
The code
Save it as recoil_kick.gd and keep one in your player script with var recoil := preload("res://recoil_kick.gd").new(). Call recoil.kick(deg_to_rad(1.5)) when a shot fires, and every frame recoil.step(delta) then camera.rotation.x = aim_pitch + recoil.offset, where aim_pitch is the pitch your mouse look already keeps. The mouse only ever changes aim_pitch, so the recoil never undoes it. For a sideways kick, keep a second one for the yaw.
extends RefCounted
## Camera recoil that kicks the view up and settles back to where the
## player is aiming now, at any frame rate. The kick is kept apart from
## the player's own aim, so mouse movement during a burst is never undone.
## var recoil := preload("res://recoil_kick.gd").new()
## when a shot fires: recoil.kick(deg_to_rad(1.5))
## every frame: recoil.step(delta)
## camera.rotation.x = aim_pitch + recoil.offset
var offset := 0.0 # radians on top of the player's aim
var settle_speed := 10.0 # higher = settles faster
var max_offset := deg_to_rad(15.0) # a long burst stops climbing here
func kick(amount: float) -> void:
offset = minf(offset + amount, max_offset)
func step(delta: float) -> float:
offset *= exp(-settle_speed * delta) # the same curve at 30 or 240 fps
if absf(offset) < 0.000001:
offset = 0.0
return offset
What we measured
10 shots 0.1 s apart, each kicking the view 1.5° up, starting at 10° up, while the player pulls the mouse down 0.4° per shot. Settle speed 10 for every method. Two runs gave the same numbers.
- Camera lerped back to 0 every frame (lerp to 0 with weight 10 × delta): 0.00° one second after the last shot at every frame rate, 6° below where the player meant to aim. 0.1 s after the last shot it showed 0.46° at 30 fps, 0.55° at 60, 0.58° at 144 and 0.62° at 240.
- Camera lerped back to the pitch it had at the first shot: 10.00° one second later, so the 4° the player pulled down was undone. 0.1 s after the last shot: 10.46° to 10.62°, depending on the frame rate.
- The script: 6.00° one second later at all four frame rates, and 6.84 to 6.87° 0.1 s after the last shot. Forty kicks with no settling stopped at the 15° cap.
Why it happens
When the camera's rotation is both the player's aim and the recoil, anything that pulls it back cannot tell the two apart. Lerping to 0 also fights the player whenever they look up or down without shooting, and remembering the start pitch only works if the player keeps the mouse still, which nobody does during a burst. A lerp with speed × delta as the weight also settles a different amount per second at each frame rate. exp(-speed * delta) gives the same curve at any frame rate; the small spread left in the script's 0.1 s numbers comes from the shots landing on slightly different frames.
What to change
- A rifle that should fire at the same rate on every machine: a fire rate that holds at any frame rate. Shots that land where the crosshair is, even with the view kicked up: raycast from the camera, not the barrel.
- A shake on top of the kick for explosions: 3D camera shake that leaves the aim alone.
What the shots look like: FPS Gun VFX for Godot 4 has 39 ready-made shooter effects in Modern and Retro PSX looks, including muzzle flashes with a real light, tracers, shell ejects and bullet impacts for seven surfaces.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: FPS Gun VFX for Godot 4 has 39 ready-made shooter effects in Modern and Retro PSX looks, including muzzle flashes with a real light, tracers, shell ejects and bullet impacts for seven surfaces.
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
- A fire rate that holds at any frame rate
- 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