← Pixel Dreamer Studio

A sword hitbox that hits each enemy once per swing

Keep a list of who this swing has hit, with the wielder on it from the start.

Keep a list of the bodies the current swing has already hurt, clear it when a new swing starts, and put the character holding the blade on it from the start. In our Godot 4.7 test a blade that damaged everything in get_overlapping_bodies() each physics frame hit the enemy 7 times in one 0.3 second swing, and hit the player holding it 18 times. The script below hit the enemy once per swing and the wielder never.

The code

Put this on an Area3D on the blade, with a CollisionShape3D the size of the blade, and set wielder to your character. Call swing() when the attack's active frames start and stop() when they end, for example from a method track in the AnimationPlayer. Connect hit to play your slash spark.

extends Area3D
## A sword or claw hitbox that hurts each enemy once per swing.
## Put it on the blade with a CollisionShape3D. Call swing() when the attack starts
## and stop() when it ends. Enemies need a take_damage(amount) method.

signal hit(body: Node3D)  ## play your slash spark here

@export var damage := 25.0
@export var wielder: Node3D  ## the character holding the blade, so it never hurts itself

var _swinging := false
var _hit := []  # who this swing has already hurt


func _ready() -> void:
	monitoring = true


func swing() -> void:
	_hit.clear()
	if wielder:
		_hit.append(wielder)
	_swinging = true


func stop() -> void:
	_swinging = false


func _physics_process(_delta: float) -> void:
	if not _swinging:
		return
	for b in get_overlapping_bodies():
		if b in _hit or not b.has_method("take_damage"):
			continue
		_hit.append(b)  # once per swing, however many frames the blade stays inside
		b.take_damage(damage)
		hit.emit(b)

What we measured

A blade 1.2 m long, from 0.3 m to 1.5 m in front of the shoulder, sweeping from 60 degrees right to 60 degrees left in 0.3 seconds. An enemy capsule stood 1.5 m in front, and the wielder's capsule at the shoulder.

Two runs gave the same numbers.

Why one cut becomes a burst of hits

get_overlapping_bodies() says who is touching the blade right now, and a blade stays inside an enemy for several physics frames while it sweeps through. Damaging on every frame turns one cut into a burst of hits, and the count changes with the swing speed and the physics rate. The list makes it one hit per swing. The blade also starts right next to the wielder's own body, so without the wielder on the list it cuts the player too.

What to change

Ready-made slashes: Japanese VFX for Godot 4 has 37 presets, including a Katana Slash crescent with sparks in White, Crimson and Azure, plus kitsune fox fire, torii portals, ofuda barriers, sakura petals and a shinobi smoke poof.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Japanese VFX for Godot 4 has 37 ready-made Japanese effects, including a Katana Slash crescent with sparks, kitsune fox fire, torii portals, ofuda barriers and sakura petals.

More how-tos for Japanese VFX for Godot 4: Keep attack presses made just before the swing ends

More how-tos: Hazards, traps and attacks