Godot Version
4.5.1
Hey everyone,
I’m running into an issue with my hit detection and I’m hoping someone has an idea on how to fix it without making things too heavy.
Problem
When my hitbox moves very fast, the collision is detected too late and the impact direction ends up being wrong.
It looks like the physics step misses the moment when the hitbox was actually in front of the enemy, so by the time the hit triggers, the hitbox is already behind them.
This results in the knockback direction being completely reversed.
I’ll add an image here showing the issue:
Current Implementation
Here’s how my hit logic currently works:
1. Hurtbox detects the hit and calls take_damage with the hitbox as the source
# health_component.gd
func _on_hurtbox_area_entered(area: Area2D) -> void:
if area.has_method("get_damage"):
var dmg = area.get_damage()
take_damage(dmg, area)
func take_damage(amount: int, from: Node = null) -> void:
if invincible:
return
current_health -= amount
emit_signal("damaged", amount, from)
2. Enemy listens to the damaged signal and uses the hitbox position to calculate knockback
# enemy.gd
func _on_health_component_damaged(_amount: int, from: Node) -> void:
if from is Node2D:
var source_pos = from.global_position
var dir = (global_position - source_pos).normalized()
_knockback_velocity = dir * knockback_strength
Issue with this approach
Because from.global_position is sampled at the time the hit is detected, a very fast-moving hitbox is often already behind the enemy.
That causes:
- reversed knockback
- wrong damage direction logic
- inconsistent behavior depending on speed and framerate
What I’ve Considered (but haven’t tried yet)
I’ve seen a few possible approaches—like using shape-casts/raycasts for “swept” hit detection, enlarging the hitbox, or switching from Area2D signals to manual physics queries—but I haven’t implemented them yet because they seem more time-consuming, may introduce extra performance overhead, or could complicate my current hitbox/hurtbox architecture without guaranteeing a fix. I’d like to understand which approach is actually worth the effort before refactoring the system.
Question
Has anyone dealt with this before? What are some good approaches to:
- Detect hits earlier even at high velocities
- Get the true point or direction of impact
- Keep the solution lightweight / performant
Any ideas, patterns, or alternative approaches would be super helpful!
Thanks in advance!
