This is my shoot function:
(I used a Node2D for a shooting point as suggested in a thread)
func shoot():
var rocket_instance = rocket_scene.instantiate()
rocket_instance.global_position = shooting_point.global_position
print(global_position)
print(shooting_point.global_position)
print(rocket_instance.global_position)
add_child(rocket_instance, true)
print(global_position)
print(shooting_point.global_position)
print(rocket_instance.global_position)
This is the console result:
global pos
(564.1352, 342.0016)
shooting point pos
(611.1352, 342.0016)
rocket_instance pos
(611.1352, 342.0016)
global pos
(564.1352, 342.0016)
shooting point pos
(611.1352, 342.0016)
rocket_instance pos
(1175.27, 684.0033)
As you can see rocket_instance.global_position
changed after I added the child.
Can you explain why the add_child()
method will change the instance’s position?
If I use add_child()
first then change the position with rocket_instance.global_position = shooting_point.global_position
it works as I intended
I think the rocket_instance’s position is different because the add_child()
will add the player’s location on top of the shooting_point.global_position
.