So, I have a KinematicBody2D as a player's soldier, and another one as an enemy. I have all nodes of every single friendly soldier and every single enemy stored in two global arrays.
I want to have a soldier, which upon entering a "detection radius" would start firing at the enemy sprite. I tried this:
# Spotting an enemy and firing
for i in gv.enemies: #gv is global variables
var enemy = i
if (position.x - i.position.x) * (position.x - i.position.x) + (position.y - i.position.y) * (position.y - i.position.y) < detection_radius * detection_radius: #if soldier is in range of any of the enemies
while (position.x - enemy.position.x) * (position.x - enemy.position.x) + (position.y - enemy.position.y) * (position.y - enemy.position.y) < detection_radius * detection_radius: #if soldier is still in range of the enemy
var enemy_angle = enemy.position - self.position
rotation = enemy_angle.angle() + deg2rad(90)
if (bullets > 0 && canFire):
rotation = trans.angle() + deg2rad(90)#
Surprisingly, when I run this code, game lags at the start and doesn't begin. I've noticed that's something with the "while" block.
Anybody has idea how to solve this?