Skip to content
GodotComponents
← Back to components
An orange capsule on the end of a bar, inside a translucent red hitbox shape, touching a green capsule that stands inside a translucent red cylinder.

Hitbox

Type: 3D

An Area3D that detects when it touches a Hurtbox and reports the hit to it. Put it on weapons, bullets and enemy attacks.

Setup

  1. Add the Hurtbox component to your project first. Hitbox needs it.
  2. Save the script as hitbox.gd.
  3. Add an Area3D to your weapon or bullet, attach the script and give it a CollisionShape3D.
  4. Set the Collision Mask of the Hitbox to the layer your Hurtboxes are on.
  5. Set Damage in the Inspector. It is passed along with each hit; what happens with it is up to whatever listens to the Hurtbox.

Code

GDScript
class_name Hitbox
extends Area3D
## Reports a hit to any Hurtbox it overlaps.

## Passed to the Hurtbox with each hit.
@export var damage: int = 10


func _ready() -> void:
	area_entered.connect(_on_area_entered)


func _on_area_entered(area: Area3D) -> void:
	var hurtbox := area as Hurtbox
	if hurtbox:
		hurtbox.take_hit(damage)

Usage example

GDScript
# sword.gd
# Only register hits while the swing animation is playing.
@onready var hitbox: Hitbox = $Hitbox


func start_swing() -> void:
	hitbox.monitoring = true


func end_swing() -> void:
	hitbox.monitoring = false