Why is the bullet not spawning?

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

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.

Try
Add to bullet scene

example
bullet scene

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

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

ramazan | 2022-04-14 13:35

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.

SpaceIsHere | 2022-04-14 15:31

:bust_in_silhouette: Reply From: SpaceIsHere
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.