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.
- The script, with the follow code after it or before it: 22,674 pixels changed during the shake and 0 after it. The camera's position was still (0, 1.2, 6).
position += random(up to 5 cm a frame) with the follow code after it: 0 pixels changed during the shake. With the follow code before it: 450, because each nudge only lasted until the next frame's follow.- The same nudges with no follow camera, for half a second at 60 fps: 4,563 pixels still different after it stopped, with the camera 11.3 cm away. At 30, 60, 144 and 240 fps it ended 9.7, 11.3, 34.8 and 35.5 cm away.
- A random offset every frame for 20 frames: the shake lasted 0.67 s at 30 fps, 0.33 at 60, 0.14 at 144 and 0.08 at 240, and changed direction 16.5, 33, 79 and 132 times a second.
- The script with
shake(1.0): 0.67 s at all four frame rates. It changed direction 31 to 33 times a second at 60, 144 and 240 fps and 22.5 at 30 fps, where a frame every 33 ms skips some of the wobbles.h_offsetandv_offsetended on exactly 0, and the position and rotation never changed. Threeshake(0.6)calls in a row stopped at 1.0. - The script with FastNoiseLite's default fractal noise left on: it changed direction 21, 31.5, 52 and 67 times a second at 30, 60, 144 and 240 fps.
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
max_offset: metres the view slides at full shake. With (0.25, 0.15) the biggesth_offsetwe saw was 0.17 to 0.22 m. The view slides rather than turns, so things far away move less than things up close.decay: how much shake goes away per second. At 1.5 a full shake lasts 0.67 s. The strength is trauma squared, soshake(0.3)on its own is a gentle 9% of a full shake.wobbles: how fast it wobbles. Set it before the node enters the tree.
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
- A swing that settles the same at any frame rate
- Screen shake that returns to center
- Hit stop and slow motion
- Confetti particles with random colors
- Choppy particles: fixed_fps and interpolate
- Play animations in 12 steps a second, at the right speed on any screen
- Keep a smooth follow camera, but jump with the player when they teleport
- Make a harvested crop pop out in an arc
- Shake a rock or tree when hit, without it drifting
- Pickups that fly to the player in the same time at any frame rate
- A cooldown that waits while the game is paused
- Keep attack presses made just before the swing ends
- A dash that covers the same distance at any frame rate
- A hold-to-craft bar that takes the same time at any frame rate and finishes once