New Instance is Offset

v4.3 Godot Version

Im creating a collectathon game, and using Marker2Ds to spawn the collectables. I tried moving markers in the 2d scene view to set new spawn locations.
The issue is that a new instance is offset from Marker2D by roughly twice the distance from the center of a scene.

extends Marker2D

const collectible = preload("res://collectable scenes/collectable.tscn")

func _ready():
	var new_collectible = collectible.instantiate()
	new_collectible.position = position
	add_child(new_collectible)
	GlobalCollectedAmount.costume += 1

The last line is used to change frames on a spritesheet, to make the collectible look different every time its instantiated.
Is the issue in the way position was assigned to the new instance?

Yes, children of a node inherit their parent’s position, so the global_position of this child node is position + position. If you want the child collectable to be at the parent’s position you would set it to zero. You may see this new.position = self.position code because they use add_sibling, which is useful for moving objects that should not follow what ever spawned them.

Thanks a lot for your answer!
Funny thing is that I solved this problem while the post was pending.
Explains why changing it to
‘’’ new_collectible = position - global.position ‘’’ worked.

That only works if your marker’s parent doesn’t have an altered transform of course. Definitely either use new.position = Vector2.ZERO or don’t set the position at all

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.