I want to make a system where soldiers would detect enemy soldiers if they were a certain distance from them. The soldiers are controlled by the player, and each player has their own army. They could have 5 soldiers- 400 soldiers.
The current methood I’m thinking of would be to loop through all your soldiers
if you had 99 soldiers and the enemy had 99,that would be 9,801 droids to loop through for your side. Then you multiply that number by 2 because the enemy side also has to detect your soldiers and you get 19,602. That would cause wayyyy to much lag, and I need a better way to do this.
local pos = me.Position
local closest, distance
for _,v in enemies do
local d = (v.Position-v.Position).Magnitude
if not distance or d < distance then
closest = v
distance = d
end
end
print(closest)
Just try this; if it’s too laggy (which you should measure) then you could make a grid/chunk based system where you check where each npc is and keep that information
You then loop trough the chunks; finding which one’s closest (or in a certain distance) and then loop trough the npc’s in that chunk. This altough comes at the cost of memmory