I’m curious about potential solutions how to find closest enemies to player in current scene.
currently player getting aim by clicking mouse on screen
func get_aim_direction() -> Vector3:
var direction = get_mouse_direction()
var direction_3d = Vector3(direction.x, 0.0, direction.y)
var camera_rotation = get_viewport().get_camera_3d().global_rotation.y
return direction_3d.rotated(Vector3.UP, camera_rotation)
the enemy are instantiated randomly
func _ready() -> void:
for enemy in GlobalVars.get_enemy_count():
var template = [MELEE_ENEMY, RANGED_ENEMY].pick_random()
var new_enemy = template.instantiate() as Enemy
all_enemies.append(new_enemy)
var tween = create_tween()
tween.tween_interval(2.5)
for enemy in all_enemies:
tween.tween_interval(1.0)
tween.tween_callback(add_child.bind(enemy))
enemy.defeat.connect(update_enemies.bind(enemy))
E 0:00:06:203 <anonymous lambda>: Invalid type in function 'distance_to' in base 'Vector3'. Cannot convert argument 1 from Object to Vector3.
<GDScript Source>player.gd:79 @ <anonymous lambda>()
<Stack Trace> player.gd:79 @ <anonymous lambda>()
player.gd:79 @ get_nearest_node_in_group()
player.gd:29 @ _unhandled_input()
It happens when only one enemy is in the scene. What would be guard to protect it ?