Two things go wrong when a lightning bolt, beam or rope sprite is stretched from one point to another. from.angle_to(to) is the angle between the two positions as seen from the corner of the screen, not the direction from one to the other: in our Godot 4.7 test it missed the target by up to 264 px. And a Sprite2D is centred by default, so after scaling it to the distance, half of it sits behind the caster: 150 px behind and 150 px short of the target. The script below starts the art on the caster, turns it by the direction from A to B and scales it to the distance in world space: 0 px off, also under a scaled and turned parent and for art drawn top to bottom.
The code
Save it as beam_span.gd and load it with const Beam := preload("res://beam_span.gd"). Call Beam.span($Bolt, $Caster.global_position, target.global_position) every frame the bolt is shown, so it follows both ends. The art is expected to run left to right. For art drawn top to bottom pass true as the fourth value, and the fifth makes the bolt thicker or thinner.
extends RefCounted
## Stretch a sprite (a lightning bolt, a beam, a rope) from one point to another, in world space.
## The art is drawn left to right; set vertical = true for art drawn top to bottom.
## const Beam := preload("res://beam_span.gd")
## func _process(_delta):
## Beam.span($Bolt, $Caster.global_position, target.global_position)
static func span(sprite: Sprite2D, from: Vector2, to: Vector2, vertical := false, thickness := 1.0) -> void:
var size := sprite.get_rect().size
var length := size.y if vertical else size.x
if length <= 0.0:
return # no texture yet
sprite.centered = false
# the start of the art sits on the node's origin, centred across its width
sprite.offset = Vector2(-size.x * 0.5, 0.0) if vertical else Vector2(0.0, -size.y * 0.5)
sprite.global_position = from
var angle := (to - from).angle()
sprite.global_rotation = angle - PI * 0.5 if vertical else angle
var stretch := from.distance_to(to) / length
sprite.global_scale = Vector2(thickness, stretch) if vertical else Vector2(stretch, thickness)
What we measured
A 64 by 16 px sprite stretched from a caster at (200, 300) to 4 targets in different directions, 150 to 300 px away. We worked out where the two ends of the drawn sprite landed and how far they were from the caster and the target, then drew two of them and read the screen. Two runs gave the same numbers.
- Centred, rotation from angle_to(): start up to 150 px from the caster, end up to 264.4 px from the target.
- Centred, rotation from angle_to_point() (the right direction): start up to 150 px behind the caster, end up to 150 px short of the target.
- The script: 0.0 px at both ends for all 4 targets.
- Under a parent scaled 2 times and turned 20 degrees, start set with to_local() but rotation and scale set as if there was no parent: start up to 300 px off, end up to 104.2 px. The script: 0.0 px.
- Art drawn top to bottom, 16 by 64 px, with vertical on: 0.0 px at both ends, still 16 px thick on screen.
- On screen, a bolt from x 100 to x 400: centred, all 49 pixels of a 7 by 7 box 10 px behind the caster were lit and none just before the target. The script: 0 behind the caster, 42 of 49 just before the target (the box went 3 px past the end).
Why it happens
Vector2.angle_to() treats both points as directions from (0, 0) and gives the angle between those two directions. The direction from one point to another is (to - from).angle(), which is what angle_to_point() gives in Godot 4. A centred sprite grows from its middle when you scale it, so to start at the caster the art's left edge has to be on the origin: centered = false and an offset of half the height up. Position, rotation and scale are relative to the parent, so a parent that is scaled or turned changes all three. Setting global_position, global_rotation and global_scale avoids that.
What to change
- A bolt that should end at the first wall: find the hit point first and pass it as the target.
- Bolts jumping from enemy to enemy: chain lightning between enemies. Effects that face the wrong way when the caster turns left: flip a child node, not the body.
Ready-made pixel spells: Pixel VFX Vol 3 Elemental Magic has 12 pixel art spell effects (ice, lightning, wind, poison, holy, water, earth) in 3 color sets, 64x64 and Godot 4 ready.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Pixel VFX Vol 3 Elemental Magic has 12 pixel art spell effects (ice, lightning, wind, poison, holy, water, earth) in 3 color sets, 64x64 and Godot 4 ready.
More how-tos: Auras, rings, beams and weather
- Light shaft cards without the bright edge-on line
- Space lights, posts or coins evenly along a Path3D
- Stop a big spinning texture from looking grainy when it is drawn small
- God rays without volumetric fog
- Dark horror fog that works in Compatibility
- Magic aura particles that circle a character
- Charge-up particles that fly in to the middle
- Particle ring standing up instead of flat
- 2D particles from a sprite's shape
- A glowing circle on the ground
- A loot beam that never goes flat
- Chain lightning between enemies
- A tower's attack range ring
- Enemy vision cone with a "!" when it spots you
- Rain that follows the camera
- Rain falling through the roof
- Snow particles not falling
- orbit_velocity not working in 3D: use tangential_accel
- Rain falls through the player
- Snow that stays on roofs
- Emit particles along a Path3D
- Ambient particles in many places from one emitter
- Keep a buff aura on until the last buff really runs out
- Spin a magic circle forever at the same speed
- Space flying swords, shields or orbs evenly around a character for any count
- Text around a circle with even gaps