0 votes

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
in Engine by (14 points)

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.

(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.

1 Answer

0 votes

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-

by (292 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories