← Pixel Dreamer Studio

A spell that keeps flying straight after it leaves the hand

Add the spell to the level, not to the caster, and give it the hand's global transform.

Add the spell to the level with get_tree().current_scene.add_child(), then copy the hand's global_transform to it. In our Godot 4.7 test a spell added as a child of the caster's hand, flying at 12 m/s, jumped more than 5 m sideways in one frame when the caster turned 90 degrees, then flew off in the caster's new direction. top_level kept it on course, but it was still freed with the caster. Added to the level, the spell kept flying straight and outlived the caster.

The code

Add a Node3D with this script as a child of the caster, at the hand or the tip of the staff, facing forward. Call cast(spell_scene.instantiate()) with your spell scene. The spell starts at the hand and flies the way the hand faces.

extends Node3D
## Launches spells from the hand so they fly on their own, whatever the caster does next.
## Add it as a child of the caster at the hand or staff tip and call cast(your_spell_scene.instantiate()).
## The spell flies along the way the hand faces at speed and is freed after life seconds.

@export var speed := 12.0
@export var life := 3.0


func cast(spell: Node3D) -> void:
	get_tree().current_scene.add_child(spell)  # in the level, not under the caster
	spell.global_transform = global_transform  # starts at the hand, facing the way the hand faces
	var target := spell.global_position - global_basis.z * speed * life  # -Z is forward in Godot
	var t := spell.create_tween()
	t.tween_property(spell, "global_position", target, life)
	t.tween_callback(spell.queue_free)

What we measured

A caster facing forward with the hand 0.5 m in front of it, and three spells cast at the same moment at 12 m/s: the script, a child of the hand that moves itself, and the same child with top_level on. Two runs gave the same results to within 0.1 m.

Why a child spell follows the caster

A child's position is kept relative to its parent, so every turn or step of the caster carries the spell along. top_level makes a node ignore its parent's transform, but it is still the parent's child in the scene tree, and freeing a node frees its children. A spell in the level has nothing above it but the level.

What to change

Aiming the hand at the mouse: aim a spell at the mouse in 3D. The same problem with an effect that plays when an enemy dies: particles disappear when the node is freed.

Spells to fire: Arcane VFX Vol 1 has 39 effects for Godot 4, including Energy Orbs, Spell Impacts, Magic Circles, Mana Shockwaves, Rune Beams, Hex Shields and Arcane Portals, several of them in gold, azure, crimson or emerald versions.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Arcane VFX Vol 1 has 39 ready-made magic effects for Godot 4, including Energy Orbs, Spell Impacts, Magic Circles, Mana Shockwaves and Rune Beams.

More how-tos for Arcane VFX Vol 1: Glowing particles in Godot 4 · An expanding shockwave that hits each enemy once

More how-tos: Projectiles and aiming