← Pixel Dreamer Studio

Screen shake that returns to center

Shake the offset, not the position, fade a trauma value over time, and square it.

Move offset, not position: the camera's position belongs to whatever follows the player. Keep one trauma value from 0 to 1, add to it on each hit, take some away every second, and set the offset from noise times trauma squared. When trauma reaches 0, set the offset back to zero.

The code

Put this script on your Camera2D and call shake() when something hits. A bigger hit can pass a bigger number, up to 1.

extends Camera2D
## Screen shake: call shake() on a hit. It fades out by itself and always lands back on center.

@export var decay := 2.0  # how much shake goes away per second (1.0 = full shake)
@export var max_offset := Vector2(24, 16)  # pixels at full shake

var _trauma := 0.0
var _time := 0.0
var _noise := FastNoiseLite.new()


func _ready() -> void:
	_noise.frequency = 15.0  # about 15 wobbles per second; the default 0.01 barely moves


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


func _process(delta: float) -> void:
	if _trauma <= 0.0:
		offset = Vector2.ZERO
		return
	_trauma = maxf(_trauma - decay * delta, 0.0)
	_time += delta  # by time, not by frame: same speed at 30 or 144 fps
	var power := _trauma * _trauma  # squared: big hits shake hard, small ones barely
	var n := Vector2(_noise.get_noise_2d(_time, 0.0), _noise.get_noise_2d(0.0, _time))
	offset = n * 2.0 * max_offset * power  # offset, not position: a follow script keeps working

In our test, shake(0.5) moved the view up to about 3 pixels, and the offset was back to exactly (0, 0) a quarter of a second later. The camera's position did not change at all while it shook.

The camera ends up somewhere else

That happens when the shake adds a random amount to position every frame. Nothing ever takes it back, so the random steps add up. We ran that for 20 frames with steps of up to 8 pixels, and the camera stopped 17.6 pixels away from where it started. The script above never touches position, and sets offset to zero when the shake is over.

Works with a camera that follows

We moved the camera's position every frame, like a follow script does, while it was shaking. The shake still showed, up to about 3 pixels, because it lives in offset.

Small hits stay small, big hits do not pile up

Because the trauma is squared, shake(0.2) peaked at half a pixel at most while shake(0.5) reached about 3. Three shake(1.0) calls in a row did not add up past full: the view moved up to 10 to 15 pixels across our runs and was still in 0.5 seconds, the same as one full hit.

What to change

Numbers that pop up on each hit: see floating damage numbers. A short freeze on the same hit: hit stop and slow motion.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Card Game Feel Kit for Godot 4 plays cards with swoops, burns and shatters, with damage numbers and screen shake built in.

More how-tos: Game feel