Node Local Position to Global Position

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

I have a projectile that is spawned in against a Node. Here is the rough Hierachy

World (Node)-> Player(Character2D) → Abilities (Node) > Fireball (Node2D) > SpawnPoint.
SpawnPoint contains 3 children
ProjectilePoint1
ProjectilePoint2
ProjectilePoint3

A projectile is instantiated to the SpawnPoint node and then will interpolate to either projectpoint 1 2 or 3 depending on when it spawns in.

The ‘Fireball’ node contains a script with this code to spawn in the projectile:

var bullet = projectile.instantiate()
var bullet_location = bullet_spawn_point.position
bullet.position = bullet_location
bullet_spawn_point.add_child(bullet)
set("current_projectile", 1)

The projectile itself contains this code on its ready status

func _ready():
current_spawn_id = get_parent().get("current_projectile")
spawn_position = get_parent().position
location_to_move_towards = get_parent().get_child(current_spawn_id).position

The process function on the projectile looks like this:

func _process(delta):
t += delta * 2
move_towards_projectile_spawn(spawn_position, location_to_move_towards, t)

The interpolation part works fine enough.

The problem I am having is that once it spawns in, it will do so at the local position (globally) spawning in at (0, 0) which is correct locally but globally that is the top left hand corner of the screen.

I have tried manually moving the spawn location to the global position, but that seems to have an odd effect.

	var spawn_pos_global = to_global(spawn_position)
var location_to_move_towards_global = to_global(location_to_move_towards)
print(spawn_position, location_to_move_towards)

I am having a bit of trouble spawning the bullet in at the correct location which in this context is above the character wherever the character is standing.

I have also attempted to use the global_position property as well but that seems to have no effect.

var bullet = projectile.instantiate()
var bullet_location = bullet_spawn_point.global_position

…and then do not add it as a child of the spawn point (perhaps have a bullet parent node?), which I assume moves (or set top_level = true).

spaceyjase | 2023-06-15 15:48

Thanks. I gave this a go and added it as a child of the player instead of a child of the spawn point and that seems to have done the trick.

Opache | 2023-06-16 07:42