Projectiles disappear after a while

Godot Version

4.2.2

Question

I have an issue where the projectile scenes after a short delay all disappear at the exact same time.
I cannot for the life of me figure out why, here is a list of things I have tried:
Create new scene.
Reimplement the script.
Swap scenes.
Slowing down the bullets.
Removing object pooling.

Please help T_T

I have a video that shows this but I just got here so I can’t post it :confused:
Here is a link to the video uploaded in the Godot Café discord. LINK

These are the two scripts interacting with each other to make the projectiles function:

extends Sprite2D
class_name TurretBase

@export var firepoints: Array[Node2D]
@export var projectile_scene: PackedScene
@export var firing_range: float
@export var turn_speed: float
@export var fire_rate: float
@onready var weapon: Sprite2D = $Weapon
@onready var fire_timer: Timer = $Weapon/FireTimer

var pool: Array[ProjectileBase]

var target: Node2D

var firepoint_index: int = 0
var desired_rotation: float
var can_fire: bool = true

func _process(delta: float) -> void:
    target = find_target(get_tree().get_nodes_in_group("Threat"))
    
    if target == null:
        return
    var direction = -target.global_position.direction_to(weapon.global_position)
    desired_rotation = atan2(direction.y, direction.x)
    
    weapon.global_rotation = lerp_angle(weapon.global_rotation, desired_rotation, turn_speed * delta)
    
    if not can_fire:
        return
    
    if abs(rad_to_deg(weapon.get_angle_to(target.global_position))) < 5 :
        fire()


func find_target(candidates: Array[Node]) -> Node2D:
    var node_min: Node2D = null
    var min_dist: float = firing_range
    
    for point in candidates:
        if !point.visible:
            continue
        
        var distance: float = global_position.distance_to(point.global_position)
        if distance < min_dist:
            node_min = point
            min_dist = distance
    
    return node_min


func get_firepoint() -> Node2D:
    firepoint_index = (firepoint_index + 1) % firepoints.size()
    return firepoints[firepoint_index]


func fire() -> void:
    var firepoint: Node2D = get_firepoint()
    var projectile = get_projectile()
    projectile.global_position = firepoint.global_position
    projectile.global_rotation = firepoint.global_rotation
    projectile.unpack()
    can_fire = false
    fire_timer.start(fire_rate)
    
    firepoint.get_node("Muzzleflash").show()
    await get_tree().physics_frame
    firepoint.get_node("Muzzleflash").hide()


func get_projectile() -> ProjectileBase:
    var projectile = pool.pop_front()
    if projectile == null:
        projectile = projectile_scene.instantiate()
        get_tree().root.add_child(projectile)
        projectile.originator = self
    else:
        projectile.unpack()
    return projectile


func _on_fire_timer_timeout() -> void:
    can_fire = true
extends Area2D
class_name ProjectileBase

var originator: TurretBase
@export var speed: float = 100

var query: PhysicsShapeQueryParameters2D

func _ready() -> void:
    query = PhysicsShapeQueryParameters2D.new()
    query.transform = global_transform
    query.collide_with_bodies = true
    query.shape = $CollisionShape2D.shape
    query.exclude = [self]

func _physics_process(delta: float) -> void:
    Globals.debug.track(name + " process", can_process())
    query.motion = global_transform.x * delta * speed
    global_position += global_transform.x * delta * speed
    var results: Array[Dictionary] = get_world_2d().direct_space_state.intersect_shape(query)
    if results.size() > 0:
        hit()
    

func pack() -> void:
    hide()
    set_process(false)


func unpack() -> void:
    show()
    set_process(true)


func hit() -> void:
    pack()
    originator.pool.append(self)


func _on_body_entered(body: Node2D) -> void:
    hit()

Turns out it was my enemy script trying to calculate the direction towards the center while being on the center that made it weird.
Good to know.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.