← Pixel Dreamer Studio

A scare that plays once, and only for the player

Check who walked in, remember that it fired, and stop the Area3D watching.

body_entered fires every time any physics body enters the area, so a scare connected to it plays again each time the player steps back in, and also for enemies and props that wander through. In our Godot 4.7 test an enemy walked through once and then the player walked in and out three times: the scare played 4 times. Connecting with CONNECT_ONE_SHOT played it once, but for the enemy, so the player never saw it. Checking that the body is in the player group, remembering that it fired and turning monitoring off played it once, for the player.

The code

Save it as once_trigger.gd, add your player to the group player (Node panel, Groups), and in the level's script call preload("res://once_trigger.gd").arm($ScareZone, func(body): $Scare.play()). Pass another group name as the third value, or &"" to accept any body. reset($ScareZone) arms it again, for example when the player retries from a checkpoint.

extends RefCounted
## Turns an Area3D into a trigger that fires once, and only for the player:
## a jump scare, a cutscene, a door slamming, a checkpoint message.
## body_entered fires every time any body walks in, enemies and props too.
##   OnceTrigger.arm($ScareZone, func(body): $Scare.play())
##   OnceTrigger.arm($Door, slam_door, &"player")      # group the body must be in
## Put your player in the "player" group (Node panel > Groups), or pass &"".


static func arm(area: Area3D, action: Callable, group := &"player") -> void:
	area.body_entered.connect(func(body: Node3D) -> void:
		if group != &"" and not body.is_in_group(group):
			return  # an enemy or a crate walked in: not our trigger
		if area.has_meta(&"_fired"):
			return
		area.set_meta(&"_fired", true)
		area.set_deferred("monitoring", false)  # stop watching, once this signal has finished
		action.call(body))


static func reset(area: Area3D) -> void:  # arm it again, e.g. when the level restarts
	area.remove_meta(&"_fired")
	area.monitoring = true

What we measured

An Area3D box 2 m wide. An enemy CharacterBody3D, not in any group, walked through it once, then the player, in the player group, walked in and out three times. Two runs gave the same results.

Why it happens

An Area3D reports every body that enters it, whatever it is, every time it enters. A one-shot connection only fixes the every time part: it disconnects after the first body, whoever that was. The script answers both questions, who and how often, and switches monitoring off so the area stops doing work for a trigger that is spent. It switches it off with set_deferred(), so the change happens after the signal has finished.

What to change

What the player sees when it fires: Horror Atmosphere VFX for Godot 4 has 45 ready-made horror effects in Modern and Retro PSX looks, including flickering and dying bulbs, a dying flashlight, Ground Fog, glowing eyes and ghosts.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Horror Atmosphere VFX for Godot 4 has 45 ready-made horror effects in Modern and Retro PSX looks, including flickering and dying bulbs, a dying flashlight, Ground Fog, glowing eyes and ghosts.

More how-tos for Horror Atmosphere VFX for Godot 4: Dark horror fog that works in Compatibility · Play random sounds and events without obvious repeats

More how-tos: Hazards, traps and attacks