Move the enemy's position into the attack node's own space with global_transform.affine_inverse() and compare it with the line's length and half width there. A check in world X and Z only works while the attack faces straight ahead: in our Godot 4.7 test a 10 m line aimed 45 degrees to the side missed 135 of the 143 test points inside it and hit 115 outside it, where the line had been before it turned. Checking only the distance from the line also hit enemies behind the boss and past the end, and using the drawn numbers as world meters missed most of a slam that was scaled up. The script below missed none in every case.
The code
Save it as line_zone.gd and load it with const LineZone := preload("res://line_zone.gd"). When the attack lands, check each enemy with LineZone.hits(attack, enemy.global_position, 10.0, 0.45, 0.4): the node you aim and scale, the length and half width the line is drawn with, and the enemy's body radius. It is for attacks that start at the node and point along its -Z, the way most aimed lines and look_at() work. Scale the node evenly on all three axes.
extends RefCounted
## Hit check for a line or rectangle attack (skill shot, charge, laser, ground slam)
## that starts at the attack's node and points along its -Z, at any angle and scale.
## if LineZone.hits(telegraph, enemy.global_position, 10.0, 0.45, 0.4):
## enemy.take_damage(30)
## length and half_width are the numbers on the telegraph's material, in the node's
## own meters, so turning (aim_at, look_at, rotation) or scaling the node evenly is handled.
## margin = the enemy's body radius in world meters, so a body touching a side counts.
## Height is ignored: an enemy on a step or a slope still counts.
static func hits(node: Node3D, world_pos: Vector3, length: float, half_width: float, margin := 0.0) -> bool:
var p := node.global_transform.affine_inverse() * world_pos
var m := margin / node.global_transform.basis.get_scale().x # world meters -> the node's meters
var along := -p.z
return absf(p.x) <= half_width + m and along >= -m and along <= length + m
What we measured
A skillshot line 10 m long with a half width of 0.45 m, and a ground slam 4 m long with a half width of 1.6 m, both starting at the attack's node and pointing along its -Z. We checked test points every 25 cm over a 28 m square and compared each method with the true answer, worked out from the node's forward and side directions in the world. Two runs gave the same results.
- Line facing -Z, not turned: the world X/Z box check was exactly right, which is why it looks fine in a first test. Checking only the distance from the line hit 216 points outside it, behind the start and past the end.
- Line aimed 45 degrees: the world box missed 135 of the 143 points inside and hit 115 outside. An enemy 6 m along the line was missed, and one 6 m straight ahead, where the line was before it turned, was hit. Distance from the line hit 416 points outside, including an enemy 3 m behind the boss and one 12 m out, past the 10 m end.
- Line aimed along +X: the world box missed 119 of 122 and hit 120 outside.
- Slam scaled 2 times and aimed 30 degrees: turning correctly but using 4 m and 1.6 m as world meters missed 614 of the 820 points inside, including an enemy 7 m out and one 2.5 m to the side. The world box missed 615.
- The script: 0 missed and 0 wrongly hit in all four cases. An enemy on a 1 m step, on the line, still counted.
- An enemy with a 0.4 m body, its centre 0.7 m from the middle of the line so its side touches the 0.45 m edge: missed with margin 0, hit with margin 0.4, and not hit at 0.9 m. On the scaled slam, whose side is 3.2 m out, margin 0.4 hit at 3.5 m and not at 3.7 m.
Why it happens
A box in world X and Z describes the line only while it faces -Z. Aiming turns the node, so the drawn line turns, but the numbers in the check do not. Checking only the distance to the line treats it as endless in both directions, so it also covers the ground behind the attacker and beyond the end. The length and half width on the material are measured in the node's own meters, so scaling the node stretches the drawing but not a check that uses them as world meters. affine_inverse() undoes the node's position, turn and scale in one step, so the point lands in the same space the line is drawn in and the drawn numbers work as they are.
What to change
- Circles: who is inside an AoE attack circle. Cones that see the player: an enemy vision cone. Meteors that land at random spots: random points spread evenly in a circle.
- Use the margin for the enemy's collision radius, so a body touching the side of the line is hit like the player expects.
- A laser that should stop at the first wall: a RayCast3D beam.
Ready-made telegraphs: AoE Telegraphs for Godot 4 has 35 attack warning indicators for boss fights, MOBAs and action RPGs, including skillshot, charge and laser lines and ground slam rectangles. Their line_length and half_width are the numbers to pass to the script, and they send a triggered signal at the exact moment to apply damage.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: AoE Telegraphs for Godot 4 has 35 ready-made attack warning indicators for boss fights, MOBAs and action RPGs, with a triggered signal at the exact moment to apply damage.
More how-tos for AoE Telegraphs for Godot 4: Particle ring standing up instead of flat
More how-tos: Hazards, traps and attacks
- Turn a 2D character left without the flicker
- 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 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