← Pixel Dreamer Studio

2D particles follow the player or stay behind

local_coords decides whether particles move with the node after they are born.

Put the GPUParticles2D under your character. With local_coords off, the default, each particle stays where it was born, so the character leaves a trail of dust or sparks. With local_coords on, every particle already out moves with the character, for a glow or an aura that sticks to it.

Two white boxes moving right. The top box has a small cloud of dust stuck to it. The bottom box leaves a long line of dust behind it
The script below in Godot 4.7 with the Compatibility renderer, both boxes moving right at 300 pixels per second. Top: Local Coords on. Bottom: the script as written, off.

The code

Attach this to a GPUParticles2D that is a child of your character, at its feet. The script makes a small square puff texture, so it runs as it is. The local_coords line is the fix.

extends GPUParticles2D
## A dust trail behind a moving 2D character. Add it as a child of the character, at its feet.
## local_coords off leaves each puff where it was made, so the dust stays behind.


func _ready() -> void:
	local_coords = false  # the fix: puffs stay where they were made (on = they move with the character)
	amount = 30
	lifetime = 0.8
	texture = make_puff()
	var m := ParticleProcessMaterial.new()
	m.direction = Vector3(0, -1, 0)
	m.spread = 40.0
	m.initial_velocity_min = 10.0
	m.initial_velocity_max = 30.0
	m.gravity = Vector3.ZERO
	process_material = m


func make_puff() -> Texture2D:
	var img := Image.create(10, 10, false, Image.FORMAT_RGBA8)
	img.fill(Color(0.8, 0.75, 0.65))
	return ImageTexture.create_from_image(img)

What we measured

The character moved right at 300 pixels per second for 1 second. Then we measured where the lit pixels were, in pixels from the character (minus is behind it). Two runs gave close numbers.

What to change

The 3D version, with dust that starts and stops with running: running dust particles. Sparks that keep part of the speed: inherit velocity from a moving emitter. 2D particles that land on the floor: GPUParticles2D collision.

Tested in Godot 4.7 with the Compatibility renderer.

More how-tos: Play, stop and remove effects