0 votes

Ok so I would like someone to help me understand why my code isn't working and if they can give me an answer to fix it. So basically I'm new to coding and I would some to look at my code and tell me why the bullets are not spawning.

extends KinematicBody2D

var speed = 200
onready var obj = get_parent().get_node("Player")
onready var fireDelayTimer = $fireDelayTimer
export var fireDelay: float = 1
const bulletE1 = preload("res://EnemyBullet1.tscn")

func _physics_process(delta):
    var dir = (obj.global_position - global_position).normalized()
    move_and_collide(dir * speed * delta)
    look_at(obj.position)


func firedelay():
    if fireDelayTimer.is_stopped():
        fireDelayTimer.start(fireDelay)
        fire()

func fire():
    var bullet = bulletE1.instance()
    get_parent().add_child(bullet)
    bullet.position = $Position2D.global_position
    bullet.velocity = obj.position() - bullet.position
    bullet.look_at(obj.position())

This is all the code for the enemy which I would like to get the bullets to spawn in. This is also the same code i use for the player just with a few tweaks, however only the player bullets work and not the enemy's. If someone could tell me why its not working that would be appreciated.

Godot version 3.4.4 stable
in Engine by (19 points)

Try
Add to bullet scene

example
bullet scene

func _ready():
     print("bla bla ")

The aim is to check if the bullet enters the scene.

I tried that and it didn't print it however I found out why it didn't work. It's because of these 2 lines right here

bullet.velocity = obj.position() - bullet.position
    bullet.look_at(obj.position())

I accidentally put the "position" to be written as a function when it isn't a function. But godot was not telling me that was wrong so it just doing it.

1 Answer

0 votes
Best answer
bullet.velocity = obj.position() - bullet.position
    bullet.look_at(obj.position())

I accidentally put the "position" to be written as a function when it isn't a function. But godot was not telling me that was wrong so it just doing it.

by (19 points)
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 Frequently asked questions and 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 [email protected] with your username.