Code: Select all
allies = {}
enemies = {}
for _,v in ipairs(allies) do
if v.target == 0 then
for i = 1, #enemies do
v.currentBest = getMagnitude(v.x,enemies[i].x + enemy:getWidth()/2,v.y,enemies[i].y + enemy:getHeight()/2)
if getMagnitude(v.x,enemies[i].x + enemy:getWidth()/2,v.y,enemies[i].y + enemy:getHeight()/2) >= v.currentBest then
v.target = enemies[i]
v.angle = math.atan2((enemies[i].y-v.y),(enemies[i].x - v.x))
end
end
end
end
The above code works but it does not look for the closest enemy. Instead, the allies go to the newest enemy spawned. If you can help me I would really appreciate it.