How to check for items in a certain radius

I am making a block which checks for enemy blocks in a certain range of its position.

My intention is that whenever an enemy block gets in range of it, a fight can be engaged between the fleets of ships which they control.

How do I do this efficiently?

2 Likes

You can try something like this where the block looks at every enemy block from a list and returns the closest one and if its within a certain range then it actives some script

local MaxDistance = ???
local Block  = ???
local EnemyBlock = ???

local CurrentEnemy, Dis

for i,v in EnemyBlockList do
  if not CurrentEnemy or CurrentEnemy and Dis < (Block.Position - v.Position).Magntiude then
    Dis = (Block.Position - v.Position).Magntiude
    CurrentEnemy = v
  end
end

if Dis <= MaxDistance then
    --*does stuff with CurrentEnemy*
end

I do have all my blocks already organised in a certain folder. I’ll give it a try.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.