When the trap goes active, hurt every body in get_overlapping_bodies(), then hurt bodies that enter while it is still active. body_entered only fires when a body comes in, so a player who stepped onto the trap while it was resting is never reported again. In our Godot 4.7 test a trap that did its damage in body_entered while active hit a player standing on it 0 times in 3 cycles. The script below hit them once per cycle, 3 times.
The code
Put this on an Area3D with a CollisionShape3D over the trap plate. Connect state_changed to show your warning glow on WARN and the spikes or flames on ACTIVE. Bodies that should get hurt need a take_damage(amount) method.
extends Area3D
## A spike or flame trap that goes WARN, then ACTIVE, then REST, over and over.
## When it goes ACTIVE it hurts whoever is already standing on it, then anyone who steps on
## while it is still ACTIVE. Each body is hurt once per cycle.
## Give it a CollisionShape3D. Bodies that get hurt need a take_damage(amount) method.
signal state_changed(state: String) ## show your warning glow on WARN and the spikes on ACTIVE
@export var damage := 20.0
@export var warn_time := 0.8
@export var active_time := 0.6
@export var rest_time := 1.6
var state := "REST"
var _hit := [] # bodies already hurt in this cycle
var _timer := Timer.new()
func _ready() -> void:
_timer.one_shot = true
_timer.timeout.connect(_next)
add_child(_timer)
body_entered.connect(_on_body_entered)
_go("REST", rest_time)
func _next() -> void:
match state:
"REST":
_go("WARN", warn_time)
"WARN":
_hit.clear()
_go("ACTIVE", active_time)
for b in get_overlapping_bodies(): # standing on it already: body_entered will not fire for them
_hurt(b)
"ACTIVE":
_go("REST", rest_time)
func _go(s: String, time: float) -> void:
state = s
_timer.start(time)
state_changed.emit(s)
func _on_body_entered(b: Node3D) -> void:
if state == "ACTIVE":
_hurt(b)
func _hurt(b: Node3D) -> void:
if b in _hit or not b.has_method("take_damage"):
return
_hit.append(b)
b.take_damage(damage)
What we measured
Rest 0.5 seconds, warn 0.3, active 0.3, and a CharacterBody3D capsule standing on a 2 m plate from the start, for 3 cycles.
monitoringalways on, damage inbody_enteredonly while active: 0 hits.monitoringturned on only while active, damage inbody_entered: 3 hits. Turning monitoring on did report the body already inside.get_overlapping_bodies()called right after turning it on returned 0 bodies.- The script: 3 hits, one per cycle, going WARN, ACTIVE, REST each time.
- Stepping on just after the trap went active: hit once within 0.05 seconds, and not again for the rest of that active time.
- Stepping on during the warning: no hit while it warned, one hit as soon as it went active.
Two runs gave the same counts.
Why the player on the trap is missed
body_entered and body_exited report changes, so a body that was on the plate before the spikes came up has nothing new to report. The trap has to ask who is on it at that moment. With monitoring on all the time the list of bodies inside is already filled when the trap goes active. Turning monitoring on at that moment also works, through body_entered, but the list is empty for the first frame.
What to change
- A trap that slides along a Path3D at a steady speed: move the PathFollow3D in metres.
warn_time: how long the player has to step off after the warning starts.active_timeandrest_time: how long the spikes stay up and how long the plate is safe.- For fire or gas that keeps hurting while you stand in it, use a damage-over-time zone instead.
Ready-made traps: Dungeon Trap VFX for Godot 4 has 27 presets in Stylized and Retro PSX looks, including Floor Spikes, Flame Jets, a Poison Gas Vent, a Laser Grid and Pressure Plates, with a trap script that runs the same warn, active and rest rhythm.
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
- 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
- A saw blade that slides along its path at a steady speed
- Explosion damage that reaches big enemies
- A spinning laser that hits everything it sweeps over