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.
- 0.25 s after the cast: all three 3.6 to 3.7 m ahead.
- The caster turns 90 degrees to the left: the child jumped to the caster's left side, 5.23 and 5.32 m from where it had been. The script's spell and the top_level one moved 0.2 m, their own flight in that frame.
- The caster steps 2 m to the side: the child went with it. The other two stayed on their line.
- 0.25 s later: the script's spell and the top_level one 7.0 to 7.1 m straight ahead. The child 5.0 to 5.1 m to the left, flying the caster's new way.
- The caster is freed: the script's spell was still there. The child and the top_level one were both gone. The script's spell freed itself after its 3-second life.
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
speedandlife: how fast the spell flies and how long before it is removed.- If your spell scene already moves itself in
_physics_process(), keep the first two lines ofcast()and leave out the tween. - For hits, give the spell an Area3D and free it when it touches something.
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
- A ball that hits the net however hard it is kicked
- A shot that meets a moving target
- An Area3D that does not see a bullet or another area
- A fire rate that holds at any frame rate
- Point swords, projectiles and models that face +Z at the target
- FPS shots that land on the crosshair
- Turn towards a target the short way round
- A laser beam that stops where it hits, the moment you aim
- Fast projectiles that never fly through enemies
- A trajectory line that matches where the ball lands
- Aim a spell at the mouse on a 3D floor
- A homing missile that turns, but not instantly
- Particles fall sideways when you rotate the emitter
- Particles that fly into the player
- Gaps in the trail of a fast emitter
- Make a tower shoot the enemy closest to your base
- Hit the target with an arcing shot at any angle and height
- Kick a RigidBody3D ball with the same strength at any tick rate
- Reload without losing or making up rounds
- Fan out any number of projectiles evenly around the aim direction
- Recoil that settles back to where the player aims