Enemies dying in a specific order in 3D.

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

I’m working on a game , I’m still prototyping to see what I can create it came to my attention that my enemies are dying in specific order. the first one added then second one to the last one ,
no matter if I Instantiated them
this is the code of the enemy:


var speed = 3
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var health = 100
@onready var nav_agent := $NavigationAgent3D
func update_target_location(target_location):
    nav_agent.set_target_position(target_location)

func _physics_process(delta):
    if not is_on_floor():
	    velocity.y -= gravity * delta
    var current_location = global_transform.origin
    var next_location = nav_agent.get_next_path_position()
    var new_velocity = (next_location - current_location).normalized() * speed
    
    nav_agent.set_velocity(new_velocity)
 func _process(delta):
    _kill()
    take_damage()

func _on_navigation_agent_3d_velocity_computed(safe_velocity):
    velocity = safe_velocity
    move_and_slide()
    pass

func assault_damage():
    health -= 16.567543
    if health < 0:
	    queue_free()

func pistol_damage():
    health -= 7.9039834

func _kill():
    if health < 0:
	    queue_free()

func take_damage():
    if global.assault_hit:
	    assault_damage()
	    global.assault_hit = false
    if global.pistol_hit:
	    pistol_damage()
	    global.pistol_hit = false

player code where I damage them:

func shoot():
if Input.is_action_pressed("shoot") and $neck/Camera3D/pistol/AnimationPlayer.current_animation != "kick" and pistol_visible:
	$neck/Camera3D/pistol._shoot()
	if raycast.is_colliding():
		var hit_player = raycast.get_collider()
		if hit_player.is_in_group("enemies"):
			global.assault_hit = false
			global.pistol_hit = true
if Input.is_action_pressed("shoot") and $neck/Camera3D/assault_rifle/AnimationPlayer.current_animation != "kick" and assault_rifle_visible:
	$neck/Camera3D/assault_rifle._shoot()
	if assault_raycast.is_colliding():
		var hit_player = assault_raycast.get_collider()
		if hit_player.is_in_group("enemies"):
			global.assault_hit = true
			global.pistol_hit = false

all help is appreciated and I thank you

So there’s some funky global variables here being actioned by the enemies (regardless of which one was hit). Likely they’re all eventually running _kill (ultimately queue_free) in the order they’re in the tree (which ain’t shown).

Get rid of the globals.

spaceyjase | 2023-05-18 18:53