If an attack animation switches a hitbox on with one value key and off with the next, a frame that steps over both never sees it on. In our Godot 4.7 test a hitbox on for 0.05 s was seen in 3 frames at 60 fps, 1 at 20 and 15 fps, and 0 at 12 fps or when a single frame at 60 fps took 0.1 s. A Call Method key at the same moment was called once in every case, and a hit window opened from it, which stays open for at least one check, was checked at least once every time.
The code
Save it as hit_window.gd. On the attacker, add var window := preload("res://hit_window.gd").new() and a function func open_hit_window(): window.open(0.05), and call it from a Call Method track key where the swing starts, instead of switching the hitbox with value keys. Each frame, check for hits while window.is_open(), then call window.tick(delta). Call window.close() if the attack is cancelled.
extends RefCounted
## An attack's hit window that a low frame rate or a stutter cannot skip.
## Open it from a Call Method key in the attack animation, check for hits
## while it is open, then tick it. It stays open for the given seconds, and
## always for at least one check, even if the whole window fell inside one frame.
## var window := HitWindow.new()
## func open_hit_window(): window.open(0.05) # Call Method key at the swing
## func _physics_process(delta):
## if window.is_open():
## check_hits()
## window.tick(delta)
var _left := 0.0
var _must_check := false
func open(seconds: float) -> void:
_left = seconds
_must_check = true
func is_open() -> bool:
return _left > 0.0 or _must_check
func tick(delta: float) -> void: # call once per frame, after checking hits
if is_open():
_must_check = false
_left -= delta
func close() -> void: # the attack was cancelled (stagger, dodge)
_left = 0.0
_must_check = false
What we measured
A 0.5 s swing with a value key that sets hitbox_on true at 0.20 s and false at 0.25 s, and a Call Method key at 0.20 s. The AnimationPlayer was stepped by hand with advance() at a fixed frame time, with Callback Mode Method set to Immediate, and after each step we counted whether a hit check would see the hitbox. Two runs gave the same counts.
- 144 fps: hitbox_on seen in 7 frames, the script's window checked in 8.
- 60 fps: 3 and 4. 30 fps: 2 and 2. 20 fps and 15 fps: 1 and 1.
- 12 fps: hitbox_on seen in 0 frames. The method key was called once, and the script checked once.
- 60 fps with one 0.1 s frame at 0.18 s: hitbox_on seen in 0 frames. The method key was called once, and the script checked once.
Why it happens
A value track with discrete keys sets the property to the key at or before the time the animation reached. When one frame moves the animation from before 0.20 s to after 0.25 s, the value it lands on is false, and true never happens. A Call Method track calls the keys the animation passed during the frame, so in our test the key at 0.20 s ran even in the frame that jumped from before it to after it. The script turns that one call into a window measured in seconds, and keeps it open until it has been checked at least once.
What to change
- A hitbox that should hit each enemy once per swing: one hit per swing.
- Button presses during the swing: an input buffer. A parry window that should not shrink at high frame rates: count seconds, not frames.
What the hit looks like: the Battle VFX Bundle for Godot 4 has 169 ready-made combat effects from 5 packs: gun effects, spell projectiles with cast, trail and hit, block and parry, AoE telegraphs and status effects.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Battle VFX Bundle for Godot 4 has 169 ready-made combat effects from 5 packs: gun effects, spell projectiles with cast, trail and hit, block and parry, AoE telegraphs and status effects.
More how-tos for Battle VFX Bundle for Godot 4: Fast projectiles that never fly through enemies
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
- 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 fire or poison zone that keeps hurting while you stand in it
- 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