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.
progress_ratio += 0.25 * delta: 1.00 m/s on the 4 m path, 4.98 m/s on the 20 m path and 3.36 m/s on the bend, and it jumped back to the start 4 times on each.progress += 3.0 * deltawith Loop on, the default: 3 m/s on every path, but it jumped back to the start 14 times on the 4 m path, 2 times on the 20 m path and 4 times on the bend.- With Cubic Interp on, the default, the speed from frame to frame was 2.81 to 3.49 m/s on the 4 m path, 2.66 to 3.26 m/s on the 20 m path and 2.60 to 3.44 m/s on the bend, even when the progress went up by exactly the same amount every frame.
- The script: 3.00 m/s on all three paths, turning around 14, 2 and 4 times, with no jumps.
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
- A trap that should hurt the player who is already standing on it when it turns on: check who is inside the Area3D.
- Placing torches or spikes at even steps along the same Path3D: objects evenly along a curve.
- Sparks or smoke along the path the blade follows: emit particles along a Path3D.
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
- Turn a 2D character left without the flicker
- A line or rectangle attack that hits what the player sees
- A scare that plays once, and only for the player
- An attack that hits even when a frame is slow
- Growing one cloud or aura zone grows all of them
- A parry window that is the same length on every screen
- A pair of portals that sends you through once
- An expanding shockwave that hits each enemy once
- Explosion damage that walls block, on the frame it goes off
- A fire or poison zone that keeps hurting while you stand in it
- A spike trap that hits whoever is already standing on it
- Who is inside an AoE attack circle
- Block only attacks from the front
- A sword hitbox that hits each enemy once per swing
- Slows and roots that overlap
- Blink forward without going into or through walls
- Knockback that the walk code does not cancel
- Random spots spread evenly inside a circle
- Keep a pressure plate down while anything is still on it
- Refill the guard meter only after the hits stop
- Heal from the damage that actually landed
- Let the shield take what it can and pass the rest of the hit to health
- Explosion damage that reaches big enemies
- A spinning laser that hits everything it sweeps over