← Pixel Dreamer Studio

A saw blade that slides along its path at a steady speed

Move the PathFollow3D in metres, turn it around at the ends, and turn off Cubic Interp.

Adding to progress_ratio moves a trap by a share of the path, so a long path makes it fast and a short one slow, and with Loop on, both progress_ratio and progress make it jump back to the start at the end of an open path. In our Godot 4.7 test, progress_ratio at 0.25 per second gave 1.00 m/s on a 4 m path and 4.98 m/s on a 20 m one, and progress at 3 m/s jumped back to the start 14 times in 20 seconds on the 4 m path. Counting metres, bouncing between the ends with pingpong() and turning off Cubic Interp gave a steady 3.00 m/s on every path with no jumps.

The code

Save it as moving_trap.gd and put it on the PathFollow3D under your Path3D, with the trap (a saw blade, a laser or a spike block) as a child of the PathFollow3D. Set speed in metres per second. Leave ping_pong on for a path with two ends, and turn it off for a closed loop that the trap should go round and round.

extends PathFollow3D
## Moves a trap (saw blade, laser, spike block) along its Path3D at a set speed
## in metres per second, the same on a short path and a long one, and turns it
## around at each end instead of jumping back to the start.
## Put this on the PathFollow3D, the trap as its child.


@export var speed := 3.0         # metres per second
@export var ping_pong := true    # false: go round and round, for a closed path

var _distance := 0.0


func _ready() -> void:
	loop = not ping_pong
	cubic_interp = false  # the baked points are not evenly spaced: cubic makes the speed wobble
	_distance = progress


func _physics_process(delta: float) -> void:
	step(delta)


func step(delta: float) -> void:
	var path := get_parent() as Path3D
	if path == null or path.curve == null:
		return
	var length := path.curve.get_baked_length()
	if length <= 0.0:
		return
	_distance += speed * delta
	progress = pingpong(_distance, length) if ping_pong else fposmod(_distance, length)

What we measured

A straight 4 m path, a straight 20 m path and a U-shaped bend of 13.49 m, each a Path3D with a PathFollow3D in a running scene, moved for 20 seconds at 60 fps. Speeds are the distance moved each frame, away from the ends of the path. Two runs gave the same numbers.

Why it happens

progress_ratio is the share of the path, from 0 to 1, so the same step per second covers more metres on a longer path. progress is in metres, but with Loop on, going past the end wraps it back to 0, which on a path with two ends is a jump to the start. The wobble comes from the curve itself: on our straight 4 m line, Godot baked 25 points that were not evenly spaced, the first at 0, 0.172, 0.369, 0.490, 0.625 and 0.771 m. Cubic Interp bends the position between those uneven points, so progress 0.05, 0.10 and 0.15 put the trap at 0.0367, 0.0895 and 0.1476 m. With Cubic Interp off, the same progress put it at exactly 0.0500, 0.1000 and 0.1500 m. The bake interval was 0.20 m, so with Cubic Interp off a curve is followed in straight pieces about that long.

What to change

What the traps look like: Dungeon Trap VFX for Godot 4 has 27 ready-made dungeon trap effects in Stylized and Retro PSX looks, including Floor Spikes, Flame Jets, a Poison Gas Vent, a Laser Grid and Pressure Plates.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Dungeon Trap VFX for Godot 4 has 27 ready-made dungeon trap effects in Stylized and Retro PSX looks, including Floor Spikes, Flame Jets, a Poison Gas Vent, a Laser Grid and Pressure Plates.

More how-tos: Hazards, traps and attacks