Save the node's position once and set position = rest + offset every frame, with the offset shrinking to zero. Adding a random step to position each frame is a random walk: in our Godot 4.7 test a rock shaken that way for a quarter of a second ended up 9 to 19 cm from where it started, and three quick hits left it 20 to 32 cm away. With the script below it came back to 0.0000 m every time.
The code
Add this as a child of the rock, ore or tree and call hit() from your pickaxe or axe. broken fires on the hit that reaches hits_to_break, where you can spawn the pieces and the loot.
extends Node
## Shakes a rock, ore or tree when it is hit, then puts it back exactly where it was.
## Add this as a child of the node to shake and call hit() from your pickaxe or axe.
## After hits_to_break hits it emits "broken".
signal broken
@export var strength := 0.08 ## metres at the start of the shake
@export var time := 0.25 ## seconds
@export var hits_to_break := 3
var hits := 0
var _target: Node3D
var _rest := Vector3.ZERO
var _left := 0.0
func _ready() -> void:
_target = get_parent() as Node3D
_rest = _target.position # every shake is measured from here
func hit() -> void:
hits += 1
_left = time # a new hit restarts the shake, it does not add to it
if hits == hits_to_break:
broken.emit()
func _process(delta: float) -> void:
if _left <= 0.0:
return
_left = maxf(0.0, _left - delta)
var k := _left / time # 1 at the hit, 0 at the end
var jitter := Vector3(randf_range(-1.0, 1.0), 0.0, randf_range(-1.0, 1.0))
_target.position = _rest + jitter * strength * k # k is 0 on the last frame: back at rest exactly
What we measured
A node shaken by up to 0.08 m per step, 0.25 seconds per hit. We tracked how far it got from its starting point during the shake and where it was half a second later. Ranges are from two runs.
position +=a random step every frame, one hit: up to 0.19 to 0.21 m away during the shake, 0.09 to 0.19 m away after it stopped.- The same, three hits 0.1 seconds apart: up to 0.27 to 0.44 m away, 0.20 to 0.32 m away afterwards.
- The script, one hit: up to 0.066 to 0.068 m away, 0.0000 m afterwards.
- The script, three more hits 0.1 seconds apart: up to 0.083 to 0.090 m away, 0.0000 m afterwards.
brokenfired once, on the third hit, and not again on the fourth.
Why it comes back exactly
Each frame the script places the node at the rest position plus a jitter that is multiplied by the time left. On the last frame the time left is 0, so the jitter is 0 and the node is exactly at rest. A new hit restarts the countdown instead of stacking a second shake on top, so fast swings never push it further out.
What to change
strengthandtime: a short, small shake for a stone, a longer, wider one for a big tree.- The jitter only moves on x and z, so the object never sinks into the ground. Add a y value for something hanging, like a lantern.
- If the object also moves for other reasons, update
_restwhen it does, or put the mesh in a child node and shake that.
Ready-made gathering effects: Crafting & Gathering VFX for Godot 4 has 28 presets in Stylized and Retro PSX looks, including Mine Stone, Mine Crystal, Mine Gold Ore, Chop Wood and Gather Herb, plus forge, anvil and potion effects.
For a camera shake in 2D: screen shake with Camera2D. In 3D: 3D camera shake with h_offset and v_offset. Mining or chopping by holding a button: a hold bar that finishes once.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Crafting & Gathering VFX for Godot 4 has 28 ready-made crafting effects in Stylized and Retro PSX looks, including Mine Stone, Mine Crystal, Mine Gold Ore, Chop Wood and Gather Herb.
More how-tos for Crafting & Gathering VFX for Godot 4: Count and take crafting items from every stack
More how-tos: Game feel
- A swing that settles the same at any frame rate
- Screen shake that returns to center
- 3D camera shake that lands back on 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
- 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