First game, My bullets stay in line with the player

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

So ive been trying out Godot, and I decided to try a small 2D shooter thing, just to see how to do it. There seems to be a problem that either whenever I try to shoot there is a crash, and when I fix that the bullets are attached to the player.

Main Node-------------

  • ColorRect(Just a background to stop the redghosting effect)
  • Player (instance I guess?)

Main Code-----

extends Control
export (PackedScene) var Bullet 
export (PackedScene) var bbullet
onready var gun1= $Player/Gun1
onready var gun2= $Player/Gun2

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

func shoot():
	var bullet = Bullet.instance()  
	gun1.add_child(bullet)
	bullet.set_position(gun1.get_position())
	var bbullet = Bullet.instance()
	gun2.add_child(bbullet)
	bbullet.set_position(gun2.get_position())

Player----------------

  • 3 Sets of particle 2D’s(I wanted to try it out but it not attached to
    code so it should affect anything)
  • sprite
  • Two Node2D’s Named Gun1 and Gun2 (I use these to set the position of
    the bullet children)

Player Code---------------

    extends Node2D export (PackedScene) var Bullet  export (PackedScene) var bbullet onready var gun1= $Gun1 onready var gun2= $Gun2 export var SPEED = 400; var screen_size


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

func _process(delta):
	var velocity = Vector2();
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1
	if velocity.length() > 0:
		velocity = velocity.normalized() * SPEED;
		
	position += velocity * delta;
	position.x = clamp(position.x, 0, screen_size.x);
	position.y = clamp(position.y, 0, screen_size.y);

Bullet Nodes--------------

  • Area2D named Bullet
  • Particle 2D
  • Sprite

Bullet Code----------------

extends Area2D
var bulletVelocity=100



func _process(delta):
	position.x +=bulletVelocity * delta 
:bust_in_silhouette: Reply From: A112Studio

You’re adding bullets as children od your guns, try adding them to parent scene

But I need the start position of the bullets to be the guns. How would I go about that?

ThatGNamedLoughka | 2020-05-20 19:49

I switched the shooting code to main and its exactly the same.

Main code

    extends Control
export (PackedScene) var Bullet 
export (PackedScene) var bbullet
onready var gun1= $Player/Gun1
onready var gun2= $Player/Gun2

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

func shoot():
	var bullet = Bullet.instance()  
	gun1.add_child(bullet)
	bullet.set_position(gun1.get_position())
	var bbullet = Bullet.instance()
	gun2.add_child(bbullet)
	bbullet.set_position(gun2.get_position())

ThatGNamedLoughka | 2020-05-20 19:57

I’m not the person who answered, but it appears that you missed his point. I’m also new to godot, but I hope this helps:

You are still doing gun1.add_child(bullet). Instead, your should find the node for your ‘world’ or the scene that contains everything, and add the bullet child to that - e.g., myWorld.add_child(bullet).

In order to position the bullet, you can set its origin to be equal to the origin of the gun, e.g., bullet.transform.origin = gun1.transform.origin (depending on whether you are working with things in local or global space).

MisterAcoustic | 2020-05-21 04:23

You can still use gun1.position, but instead of gun1.add_child() do get_tree().get_root().add_child(bullet)

A112Studio | 2020-05-21 12:47

@A112Studio get_root isn’t even a real method.

I tried to do what @MisterAcoustic told me but it seems that no bullets fire anymore.

This is just so confusing, I have managed to spend two days with this and nothing seems to work.

ThatGNamedLoughka | 2020-05-21 15:32

My bad! I meant get_tree().get_root() Sorry! :slight_smile:

A112Studio | 2020-05-21 19:40

get_tree().get_root() worked thanks!! Now its on main but even with the other edits when I fire it sets them from the bullets original starting position and not on the gun Nodes.

ThatGNamedLoughka | 2020-05-22 05:27

I was able to make it work by usin the following code for the placement:

func shoot(): 	var bullet = Bullet.instance()   	bullet.set_global_position(gun1.get_global_position()) 	get_tree().get_root().add_child(bullet) 	var bbullet = Bullet.instance() 	bbullet.set_global_position(gun2.get_global_position()) 	get_tree().get_root().add_child(bbullet)

ThatGNamedLoughka | 2020-05-23 01:40