Attention | Topic was automatically imported from the old Question2Answer platform. | |
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