← Pixel Dreamer Studio

A line or rectangle attack that hits what the player sees

Turn the enemy's position into the attack's own space, then compare with the length and width it is drawn with.

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.

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

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