← Pixel Dreamer Studio

Shake a rock or tree when hit, without it drifting

Offset from the rest position, not from where it is now.

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.

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

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