Set global_position after add_child(), not position and not before. position is measured from the parent, and a node that is not in the tree yet has no world to measure from. In our Godot 4.7 test an effect spawned under an enemy that was moved, turned 90 degrees and scaled by 2 landed 14.35 m from the hit point in both of those cases, and setting global_position first also printed an error. Added first, then global_position set: 0.00 m. It was still twice its size, which the script below fixes.
The code
Save it as place_at.gd and use it wherever you spawn something at a point: preload("res://place_at.gd").at(fx, enemy, hit_point). Pass true as the fourth value when the effect should stay where it was spawned instead of moving with the parent, like a blood splat on the floor.
extends RefCounted
## Adds a spawned node under any parent and puts it exactly at a point in the
## world, whatever the parent's position, rotation and scale.
## Place.at(fx, enemy, hit_point) # follows the enemy from now on
## Place.at(fx, enemy, hit_point, true) # stays where it was spawned
## keep_size = true also undoes the parent's scale, so the effect is its own size.
static func at(node: Node3D, parent: Node, point: Vector3, stay_in_world := false, keep_size := true) -> Node3D:
parent.add_child(node) # add first: global_position only means "world" once the node is in the tree
node.top_level = stay_in_world # true = ignores the parent's later moves
node.global_position = point
if keep_size:
if stay_in_world:
node.scale = Vector3.ONE # top level: its own transform is its world transform
elif parent is Node3D:
var s: Vector3 = (parent as Node3D).global_basis.get_scale()
node.scale = Vector3(1.0 / s.x, 1.0 / s.y, 1.0 / s.z)
return node
What we measured
An enemy at (5, 0, 0), turned 90 degrees and scaled by 2, and a hit at (6, 1, 2). Each line gives how far the spawned node was from the hit, its size in the world, and how far it was from the hit after the enemy walked 3 m on. Two runs gave the same numbers.
position = hit, then added under the enemy: 14.35 m off, twice its size, then 11.45 m off.global_position = hitbeforeadd_child(): the same 14.35 m, and Godot printedCondition "!is_inside_tree()" is true.- Added under the enemy, then
global_position = hit: 0.00 m, but twice its size, and 3.00 m off once the enemy moved, because it moved with it. - The script: 0.00 m and its own size, and it moved with the enemy.
- The script with stay in world: 0.00 m, its own size, and still 0.00 m after the enemy walked away.
- Added under the level instead of the enemy, then
global_position: 0.00 m, its own size, and it stayed.
Why it happens
position, rotation and scale are all relative to the parent, so a point in the world turns into a different point once a moved, turned or scaled parent applies its own transform on top. global_position converts for you, but only once the node knows its parent, which is after add_child(). Before that, Godot cannot read the world transform, prints the error and uses the value as a local one, which is why it landed in the same wrong place as position. A child also takes its parent's scale. top_level cuts the node loose from the parent's transform, so it stays put while the parent moves.
What to change
- Should it follow? A hit spark, a buff glow or a status effect on the target: follow. A crater, a splat or a ground decal: stay in the world, or add it under the level.
- Facing the surface it hit: spawn a hit effect at the impact point. An effect that should keep its size on a scaled character: scaling a 3D particle effect. Effects that vanish when the enemy is freed: particles that disappear with the node.
Ready-made effects: the VFX Collection for Godot 4 has 1,636 effects in 44 packs, and its PDVFX.spawn(scene, parent, pos) adds the effect under the parent first and then sets its global position, the order this page recommends.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: VFX Collection for Godot 4 has 1,636 effects in 44 packs, and PDVFX.spawn() adds an effect under any parent first and then puts it at the point you give it.
More how-tos for VFX Collection for Godot 4: A loot beam that never goes flat · Particles not working in a web export · Pixel art particles look blurry · Scale a 3D particle effect evenly
More how-tos: Play, stop and remove effects
- An aura that comes back when the buff comes back
- Draw the hit effect over what it hit
- Remove a one-shot particle effect when it ends
- Fade out and stop looping particles
- Spawn a hit effect facing the surface
- Hit sparks from a pool of emitters
- Changing one particle effect's color changes all of them
- Particles that play the same way every time
- Freeze particles in mid-air
- Running dust and landing puffs
- Particles left behind by a moving object
- 2D particles follow the player or stay behind
- Footstep particles that match the ground
- A level up effect that plays while paused
- Hide particle effects far from the camera
- Death particles disappear when the enemy is freed
- Remove all particles at once
- Tire smoke that only shows when the car drifts
- Play a 2D sprite sheet effect once and remove it, even if its animation loops
- Sprite sheet frames without the empty cells
- Sprite sheet frames without lines from the next cell