Use curve.sample_baked(length * i / (count - 1)) with length = curve.get_baked_length(): that walks the curve by distance. Spreading the steps over the point index with curve.sample(index, t) gives every segment the same number of objects however long it is. In our Godot 4.7 test 12 lights on a 14.47 m path with segments of 2, 8 and 4.47 m ended up 0.36 to 3.19 m apart that way, and 1.31 to 1.32 m apart with sample_baked(). On a closed 40 m loop, dividing by count - 1 put the last light exactly on the first one. Dividing by count gave 12 gaps of 3.33 m.
The code
Save it as path_spacing.gd. even_points() returns the positions, and place() adds a copy of a scene at each one as a child of the Path3D, so they sit on the curve wherever the path is. Pass closed = true for a loop whose last point is back on its first.
extends RefCounted
## Place things at even distances along a Path3D: string lights, fence posts, coins.
static func even_points(curve: Curve3D, count: int, closed := false) -> PackedVector3Array:
var out := PackedVector3Array()
var length := curve.get_baked_length()
var gaps := count if closed else count - 1 # a loop ends where it starts, so it has one gap more
for i in count:
out.append(curve.sample_baked(length * i / maxi(gaps, 1), true))
return out
static func place(path: Path3D, scene: PackedScene, count: int, closed := false) -> Array[Node3D]:
var nodes: Array[Node3D] = []
for p in even_points(path.curve, count, closed):
var n := scene.instantiate() as Node3D
path.add_child(n)
n.position = p # curve points are in the Path3D's own space
nodes.append(n)
return nodes
What we measured
Distances are measured along the curve unless we say straight. Two runs gave the same numbers.
- An open path from (0, 0, 0) to (2, 0, 0), (10, 0, 0) and (12, 0, 4), 14.47 m long, with 12 objects. Spread over the point index, each of the three segments got 4 of them: 0.36 to 3.19 m apart.
- The same path with sample_baked() by distance: 1.31 to 1.32 m apart. Measured straight, the two either side of the corner at (10, 0, 0) were 1.13 m apart, because a straight line cuts the corner.
- A curved path, two points with handles: 0.63 to 1.59 m apart by point index, 1.13 to 1.14 m with sample_baked().
- A closed square loop, 40 m: with length * i / (count - 1), light 12 landed on (0, 0, 0), on top of light 1, and the other gaps grew to 3.64 m. Dividing by count: 12 gaps of 3.33 m, the one from the last light back to the first included.
- place() on a Path3D moved to x = 50: 12 nodes from (50, 0, 0) to (62, 0, 4), on the curve where it is drawn.
Why it happens
A curve's point index counts points, not meters. A step from t = 0 to 1 covers a whole segment whatever its length, and on a curved segment equal steps in t are not equal distances either. The baked curve is the curve cut into short pieces with their lengths added up, so an offset into it is a distance. A line has one gap fewer than the objects on it. A loop that ends where it starts has as many gaps as objects, so dividing by count - 1 stretches the gaps until the last object reaches the start.
What to change
- A trap that slides along a Path3D at a steady speed: move the PathFollow3D in metres.
- Spacing in meters instead of a count: on a line, count = int(length / spacing) + 1.
- Particles along the same curve instead of nodes: emit particles along a Path3D.
Lights along a path: Winter & Christmas VFX for Godot 4 has String_Lights presets whose script builds the bulbs, the wire and a few real lights. Set its path to a Path3D and the bulbs follow the curve, spaced by distance along it, for roof edges or a spiral around a tree, with Twinkle, Chase, Alternate or Steady patterns.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Winter & Christmas VFX for Godot 4 has string lights that follow a Path3D, with the bulbs, the wire and a few real lights built for you.
More how-tos for Winter & Christmas VFX for Godot 4: Snow that stays on the roofs
More how-tos: Auras, rings, beams and weather
- Light shaft cards without the bright edge-on line
- Stretch a bolt sprite from the caster to the target
- Stop a big spinning texture from looking grainy when it is drawn small
- God rays without volumetric fog
- Dark horror fog that works in Compatibility
- Magic aura particles that circle a character
- Charge-up particles that fly in to the middle
- Particle ring standing up instead of flat
- 2D particles from a sprite's shape
- A glowing circle on the ground
- A loot beam that never goes flat
- Chain lightning between enemies
- A tower's attack range ring
- Enemy vision cone with a "!" when it spots you
- Rain that follows the camera
- Rain falling through the roof
- Snow particles not falling
- orbit_velocity not working in 3D: use tangential_accel
- Rain falls through the player
- Snow that stays on roofs
- Emit particles along a Path3D
- Ambient particles in many places from one emitter
- Keep a buff aura on until the last buff really runs out
- Spin a magic circle forever at the same speed
- Space flying swords, shields or orbs evenly around a character for any count
- Text around a circle with even gaps