Do the damage from a per-body timer that reads get_overlapping_bodies(), not from body_entered. body_entered fires once each time a body comes in: in our Godot 4.7 test a character that walked into a fire zone and stood there for 3 seconds took one hit, 5 HP. With the script below it took a hit as it walked in and one every 0.5 seconds after that: 7 hits in 3.25 seconds.
The code
Put this script on an Area3D with a CollisionShape3D child the size of your fire or gas cloud. Anything that should get hurt needs a take_damage(amount) method. For a molotov, add the zone where the bottle breaks: lifetime removes it when the fire goes out.
extends Area3D
## A fire, poison or gas zone that hurts every body standing in it, once every tick_time seconds.
## body_entered fires only once per entry, so this checks who is inside each physics frame instead.
## Give it a CollisionShape3D child. Bodies that should get hurt need a take_damage(amount) method.
@export var damage := 5.0
@export var tick_time := 0.5 ## seconds between hits on the same body
@export var lifetime := 5.0 ## seconds before the zone removes itself, 0 = stays
var _next := {} # body -> seconds until its next hit
func _ready() -> void:
if lifetime > 0.0:
get_tree().create_timer(lifetime, false).timeout.connect(queue_free)
func _physics_process(delta: float) -> void:
var inside := get_overlapping_bodies()
for b in _next.keys():
if not is_instance_valid(b) or b not in inside:
_next.erase(b) # it left: the next time it walks in, it is hit right away
for b in inside:
if not b.has_method("take_damage"):
continue
var left: float = _next.get(b, 0.0) - delta
if left <= 0.0:
b.take_damage(damage)
left += tick_time
_next[b] = left
What we measured
A CharacterBody3D with a capsule and 100 HP, and a zone with a cylinder shape of 1.5 m radius, 5 damage a hit.
body_entered, walked in and stood for 3 seconds: 1 hit, 95 HP. The signal fired 0.05 seconds after the character moved in, and never again.body_entered, zone added on top of a character already standing there, like a molotov landing at its feet: it still fired, once.get_overlapping_bodies()called in the same frame the zone was added returned 0 bodies. It fills in after the next physics frames.- The script, walked in and stood: 6 hits after 3 seconds, 7 after 3.25 seconds, 65 HP.
- Walked out: no hits in the next 2 seconds. Walked back in: hit again 4 physics frames later.
- Zone added on top of the character with
lifetime2: 2 hits in the first second, the zone was gone by 2.5 seconds and there were no more hits. - A StaticBody3D without
take_damage()inside the zone, and a character freed while standing in it: no errors.
Two runs gave the same numbers.
Why body_entered is not enough
body_entered and body_exited report changes. A body that stays inside makes no more signals, so damage done there happens once per entry, however long the body stands in the fire. get_overlapping_bodies() returns who is inside right now, kept up to date by the physics step, so it can be asked every frame. Each body gets its own countdown, so one that steps in late is not hit early or late because of another.
What to change
damageandtick_time: a campfire might do 2 a second, a toxic cloud 5 every half second.lifetime: 0 keeps the zone forever, for a toxic pool or a fire trap that never goes out.- Leaving and coming back in hits right away, so a character cannot dodge damage by stepping in and out.
Ready-made survival effects: Zombie Outbreak & Survival VFX for Godot 4 has 25 presets in Stylized and Retro PSX looks, including a Molotov that burns for 5 seconds, Infection Fog, Spore Cloud, a Road Flare and Signal Smoke.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Zombie Outbreak & Survival VFX for Godot 4 has 25 ready-made zombie survival effects in Stylized and Retro PSX looks, including a Molotov that burns for 5 seconds, Infection Fog, Spore Cloud, Road Flare and Signal Smoke.
More how-tos for Zombie Outbreak & Survival VFX for Godot 4: Keep a chasing horde from stacking into one enemy · An enemy that dies once, however many hits land
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 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
- 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