Position2D does not instantiate the object

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

I want the bullet or missile to be instantiated exactly where the “Position2D” Node is, the problem is that when I set the bullet as a child of Position2D, godot instantiates it for me somewhere else. I wonder how this is possible… how can I instantiate it where Position2D is?

:bust_in_silhouette: Reply From: gioele

It seems strange, it should add them where the Position2D node is placed.

But I usually do not ad new instances as child of a position, I usually just get its global position and add them to a Node in the scene. Like this:

var bullet = bullet_scene.instance()
add_child(bullet) # I do this on the scene but you can try calling it on position
bullet.global_position = $Position2D.global_position

When I got problems, it was because I messed the order of add_child and position settings.

I tried as you suggested but it didn’t work. If you want I list the tree of nodes that I have in the scene so you get a better idea: platform(staticbody2d) that has as children = spawnposition(position2d), sprite, collisionshape(collisionshape2d), dangerarea(area2d) that has as children = collisionshape(collisionshape2d), then always as children of platform I have = timer. Then I have the bullet scene which is separate from this scene(platform) of course.

Anyway as you wrote the example above I did: $spawnposition.global_position = missile.global_position(I instantiated the missile in the variable “missile”). I don’t know… if that’s not correct, please correct me.

Peppe | 2021-10-15 08:34

:bust_in_silhouette: Reply From: Ursus_Z

It sounds like your bullet scene might not be at origin.
When you open the bullet scene, its position (under “Transform”) should be at origin (x=0, y=0), and its children (sprite, etc) should also be at origin.

Edit: lol, took about 20 hours for my answer to be approved, so I’m too late!

:bust_in_silhouette: Reply From: Peppe

So… for those who find themselves in the same situation as me, that is, not being able to instantiate an object in the desired position, the solution is to position the object or scene to be instantiated in the point (0,0) and then add it as I did before, that is, adding it as a child to the desired object or scene. I had already done this last time but then I forgot. I hope to have been of help to who like me has found himself in this difficulty.