0 votes

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?

Godot version 3.3.4
in Engine by (54 points)

3 Answers

0 votes
Best answer

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.

by (54 points)
0 votes

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.

by (292 points)

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.globalposition = missile.globalposition(I instantiated the missile in the variable "missile"). I don't know... if that's not correct, please correct me.

0 votes

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!

by (14 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.