← Pixel Dreamer Studio

A spawned effect that appears exactly where you hit

Add the node to its parent first, then set global_position, and decide whether it should follow the parent.

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.

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

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