← Pixel Dreamer Studio

A spike trap that hits whoever is already standing on it

Hurt the bodies already on it when it goes active, then anyone who steps on.

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.

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

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