I am making my first game, and running into some problems....

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

So, I am making a Galaga clone, and am trying to figure out the system for making the player shoot bullets.

Player scene tree:
-Area2d(named Player)

  • CollisionShape2D
  • Sprite
  • Bullet(scene)

Bullet scene tree:

  • Area2D(named Bullet)
  • Sprite
  • CollisionShape2D
  • VisibilityNotifier2D

Here is my code for the player:

extends Area2D

signal onBulletSpawn

var Bullet = preload("res://Bullet.tscn")
export (NodePath) var target_node_nodepath = null
var target_node
export var playerSpeed = 400
var screen_size

func _ready():
	screen_size = get_viewport_rect().size

func _process(delta):
	var Playervelocity = Vector2()
	if Input.is_action_pressed("ui_right"):
		Playervelocity.x += 1
	if Input.is_action_pressed("ui_left"):
		Playervelocity.x -= 1
	if Input.is_action_just_pressed("ui_select"):
		var bullet = Bullet.instance()
		bullet.position = position
	position += Playervelocity * delta * playerSpeed
	position.x = clamp(position.x, 0, screen_size.x)

Here is my code for the bullet:

extends Area2D

export var bulletSpeed = -10000

func _on_VisibilityNotifier2D_screen_exited():

func _process(delta):
	position += Vector2(0.0, bulletSpeed * delta)

So this all works fine, but when when I shoot a bullet, then move my character, the bullet moves with the character… Any suggestions on how to fix this?

:bust_in_silhouette: Reply From: kidscancode

You’re adding the bullet as the child of the player, so naturally it’s going to move with the player.

Instead, you should add your bullets to the world. This could be as simple as get_parent().add_child(bullet), or you can use get_node() to use whichever node you want to hold the bullets.

You both are the best answer… lol thnx!

Fizzleburn | 2020-05-20 18:52

:bust_in_silhouette: Reply From: deaton64


It’s because the bullet is a child of the character. You need to add the bullet to the parent scene, like this:


Thnx for the answer!

Fizzleburn | 2020-05-20 18:52