My scene node is shooting the bullet instead of my player node, why?

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

I’m a beginner so I was following a tutorial of a shooter game, everything is working well, but my bullets don’t come from the player, they come from de scene node. I check the scripts again and again but can’t find where the problem is.

My scripts:

Arena script

extends Node2D

func _ready():
	Global.node_creation_parent = self

func _exit_tree():
	Global.node_creation_parent = null

global script

extends Node

var node_creation_parent = null

func instance_node(node, location, parent):
	var node_instance = node.instance()
	parent.add_child(node_instance)
	node_instance.global_position = location
	return node_instance

Player script

extends AnimatedSprite

var speed = 150
var velocity = Vector2()

var bullet = preload("res://scenes/bullet.tscn")

var cant_shoot = true

func _process(delta):
	velocity.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
	velocity.y = int(Input.is_action_pressed("move_down")) - int(Input.is_action_pressed("move_up"))
	
	velocity = velocity.normalized()
	
	global_position += speed * velocity * delta
	
	if Input.is_action_pressed("click") and Global.node_creation_parent != null and cant_shoot:
		Global.instance_node(bullet, global_position, Global.node_creation_parent)
		$Reload_speed.start()
		cant_shoot = false



func _on_Reload_speed_timeout():
	cant_shoot = true

I cannot reproduce your problem. The code you provided works fine for me and spawns the bullet scene where the player is located when pressing “click”.

Maybe you can provide an example project? The issue could also be with your Bullet-scene and the script attached to it - if there is any, you haven’t provided it.

njamster | 2020-06-14 18:25

(Sorry, i didn’t see your comment before).
Well, I solve the problem by deleting Player scene of Arena scene and reinstencing it.
Still don’t undertand what was the problem, but everything is working fine now.

luigami | 2020-06-14 21:30

:bust_in_silhouette: Reply From: MaaaxiKing

First of all, we don’t see where you declared global_position. I think if your player is a child of Arena(Node2D), you have to do Global.instance_node(bullet, global_position, self) or the same:

Global.node_creation_parent = self 
Global.instance_node(bullet, global_position, Global.node_creation_parent)

If the player is not a child of Arena, instance the player there. But the best is if you can give us the link to the tutorial. This would be easier to follow-

I was following this tutorial

My player is a scene and I already instenced in the arena.
I’m using animatedsprite instead of sprite node. I don’t think this is the problem, but as a beginner I don’t really know.
After having this problem I continued the tutorial thinking I could solve it later, and a add an enemy. Now the enemy is shooting and just walk when the player walk when it should chase the player.
I’ll rewatch the tutorials and compare to my project, maybe redo it to see what i did wrong.

luigami | 2020-06-14 19:48