Skip to content
GodotComponents
← Back to components

Procedural Stop Motion/Shaking

Type: UI

Randomly jitters a Control node's position and rotation at a set frame rate. Turning the effect off in code automatically resets the node's offset transform. Inspired by the wiggle expression in After Effects.

Setup

  1. Attach the script to a Control node.
  2. Change settings to your liking, even at runtime.

Code

Released under CC0 1.0 licence (opens in a new tab): use, change and share it in any project, without asking and without giving credit.

GDScript
extends Control

@export var is_playing : bool = true:
	set(state):
		is_playing = state
		if !is_playing: reset_offset()
	get:
		return is_playing

## How many times a second should we randomize this node's position and rotation?
## 4-8 FPS recommended for a solid 'stop motion' feel.
@export_range(2,30) var fps : int = 6
@export_range(0,10) var max_position_variance : float = 1
@export_range(0,10) var max_rotation_variance : float = 1

var _timer : float = 0

func _ready() -> void:
	offset_transform_enabled = true
	_setup.call_deferred()

func _setup():
	pivot_offset = size / 2

func _process(delta):
	if !is_playing:
		return
	
	if _timer <= 0:
		offset_transform_position = Vector2(randf_range(-max_position_variance,max_position_variance),randf_range(-max_position_variance,max_position_variance))
		offset_transform_rotation = deg_to_rad(randf_range(-max_rotation_variance, max_rotation_variance))
		_timer = 1.0 / fps
	else:
		_timer -= delta

func set_is_playing(state:bool) -> void:
	is_playing = state

func reset_offset() -> void:
	offset_transform_position = Vector2.ZERO
	offset_transform_rotation = 0