Why does an object that appears in the correct place in the editor teleport to origin on game start

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MicahSchultz

I have an area 2d with a sprite child, collision polygon child, and animation player child. When I add this object to my game via the editor at, e.g., x = 30 and y = 70, it appears there in the editor, but when I run the game, it sets its position to 0,0.

Is the Area2D the root of the scene? Or is it a child of another node? Is there code defining what these nodes do? Is the position of the node the global position, or the relative position?

Ertain | 2023-01-29 03:30

:bust_in_silhouette: Reply From: samjmiller

Double check the code; if you have a script attached to the scene it may contain code that re-sets the object’s location at runtime…

For example in the script attached to my opening level, the _ready() function contains this line:

func _ready():
	player.position = Vector2(0,0)

Which will move the player from wherever I’ve positioned them on the map, and set them to the center of the scene. Maybe there’s something that is changing the position, or reparenting it to something with a different location?

I just had this issue myself and in my case it happened because in the AnimationPlayer I was animating the position property of the root Area2D node instead of the child Sprite2D. In other words, the animation was setting the global position to (0, 0) instead of the local offset position.

Old thread, I know, but this might help someone else who lands here with the same issue.

3 Likes

Here one year later to say you saved my life with this answer. I’ve been working on it for 2 straight days and couldn’t figure it out. Thank You so much!!!