← Pixel Dreamer Studio

Recoil that settles back to where the player aims

Add the kick on top of the player's aim and let only the kick fade.

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.

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

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