Trying to make the bullet spawn on the ship.

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

Heya, i’m trying to make a space invader type game but when i try to spawn the bullet it doesn’t spawn where i want it to, instead it spawns in a set location, i’m trying to get it to spawn ontop of the playership.
here’s my code as of now.
Game:

  extends Node2D

var BulletScene = preload("res://PlayerBullet.tscn")
onready var ship = $PlayerShip

func spawn_bullet():
  var bullet = BulletScene.instance()
  bullet.global_position = $PlayerShip.position
  add_child(bullet)

func _process(delta):
	if Input.is_action_pressed("shoot"):
		spawn_bullet()

What do i do?

:bust_in_silhouette: Reply From: Cire_arievilo1

place a Position 2D node on top of your ship and then in code change this:

func spawn_bullet():
  var bullet = BulletScene.instance()
  bullet.position = $Position2D.position
  add_child(bullet)