← Pixel Dreamer Studio

3D camera shake that lands back on center

Shake h_offset and v_offset, not the camera's position or rotation, and fade a trauma value over time.

Shaking a Camera3D by moving its position fights whatever else moves the camera. In our Godot 4.7 test, a follow camera that set the position after the shake erased it completely: 0 pixels changed on screen. With nothing pulling it back, half a second of random nudges left the camera 11.3 cm off at 60 fps and 35.5 cm off at 240 fps. The script below shakes h_offset and v_offset instead. They slide the view without touching the camera's transform, so the shake showed with the follow code running before it or after it, and the picture after the shake matched the one before it pixel for pixel.

The code

Add a Node as a child of your Camera3D, name it Shake and attach this script. Call $Camera3D/Shake.shake(0.5) on a hit or an explosion, and shake(1.0) for the biggest ones. Your camera keeps its own script: the shake never writes its position or rotation.

extends Node
## 3D camera shake that leaves the camera's position and rotation alone.
## Add it as a child of your Camera3D and call shake() on a hit or an explosion.
## It only moves h_offset and v_offset, so a follow script or mouse look keeps full
## control of the camera, and the view lands back exactly where it was.

@export var decay := 1.5  # how much shake goes away per second (1.0 = full shake)
@export var max_offset := Vector2(0.25, 0.15)  # metres the view slides at full shake
@export var wobbles := 15.0  # how fast it wobbles, the same at any frame rate

var _trauma := 0.0
var _time := 0.0
var _noise := FastNoiseLite.new()
@onready var _camera := get_parent() as Camera3D


func _ready() -> void:
	_noise.frequency = wobbles  # the default 0.01 barely moves
	_noise.fractal_type = FastNoiseLite.FRACTAL_NONE  # one smooth wobble: the default adds fine jitter that shows more at high frame rates


func shake(amount := 0.5) -> void:
	_trauma = minf(_trauma + amount, 1.0)  # explosions add up, but never past full


func _process(delta: float) -> void:
	if _trauma <= 0.0:
		return  # not shaking: the camera's offsets are left alone
	_trauma = maxf(_trauma - decay * delta, 0.0)
	_time += delta  # by time, not by frame
	var power := _trauma * _trauma  # squared: big hits shake hard, small ones barely
	_camera.h_offset = _noise.get_noise_2d(_time, 0.0) * 2.0 * max_offset.x * power
	_camera.v_offset = _noise.get_noise_2d(0.0, _time) * 2.0 * max_offset.y * power
	# the frame _trauma reaches 0, power is 0 and both offsets land on exactly 0

What we measured

Four boxes, with the camera 6 m back and a follow camera that put it back on the same spot and turned it to the target every frame. We ran 10 frames of a full shake at 60 fps, took a picture, ran it until it ended, took another, and counted the pixels that differed from the picture before the shake. For the frame rates we stepped each method with a fixed delta. Two runs gave the same numbers.

Why it happens

A follow camera or mouse look writes the camera's position or rotation every frame. When the shake writes the same thing, whichever runs last wins, so the shake is either wiped out or added on top for good, depending on the order of your scripts. h_offset and v_offset are separate: Godot adds them along the camera's own sideways and up axes when it draws, so they never move the node and nothing else in your game sets them.

Random numbers every frame shake faster at a higher frame rate, and counting frames makes the shake shorter. The script uses time for both. It also turns off the noise's fractal layers with FRACTAL_NONE: the extra fine layers add small wiggles that a high frame rate catches and a low one misses.

What to change

A shake for a 2D game: Camera2D screen shake. Gun recoil that should not undo the player's aim: recoil kept apart from the aim. A follow camera that slides across the level after a teleport: follow camera after a teleport. A short freeze on big hits: hit stop.

Explosions worth shaking the camera for: Modern Battlefield VFX for Godot 4 has an artillery barrage, an airstrike and a tank cannon, in Stylized and PSX looks.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Modern Battlefield VFX for Godot 4 has an artillery barrage, an airstrike and a tank cannon, in Stylized and PSX looks.

More how-tos for Modern Battlefield VFX for Godot 4: Explosion effect in one burst · 3D debris particles not rotating · Explosion damage that walls block, on the frame it goes off · Explosion damage that reaches big enemies

More how-tos: Game feel