So I’ve got a game where. Or well let me just simplify this.
There is 2 teams. Each team has 5 units, each team spawns at the other end of the map.
Each teams 5 units (Humanoids) will then walk to the other side, now I need to do checks to see if they are near each other (within like 500 studs of each other) and then order them to move to each other and attack.
But what I wonder is, what is the most efficient way of doing this? Cause the game will have more than 5 units.
If I’m checking the surroundings of 1 unit (possibly 50+ times) a second, I’m gonna guess that will be bad. So I’m wondering how I should go about this? Thank you/
Maybe a invisible no-collide part welded to the player’s HumanoidRootPart that checks if they’re touching another part like the one welded to the player. That might work.
Side note: Also very honored to get replied by a game developer with 1M total visits.
Side note 2 the electric bogaloo: Good luck on remaking Zombie Mall!
Yea that was one of my thoughts. Thanks. I’ll wait and see if some more people respond and get a ton of input if I can before doing this. Since it’s going to be pretty scaled up with up to 100 units.
Checking the surroundings of units individually is not ideal. However, checking the surroundings of units using an ipairs() or pairs() loop may be a solution. Maybe this will help:
for _, team in ipairs(teams) do
for _, unit in ipairs(team) do
task.spawn(function()
-- Check if the enemy is close; attack.
end
end
end
So I just add all the rootparts to a table and iterate through them checking each ones magnitude? Cause that sounds kinda intensive isn’t it? Looping through lets say 100, and then doing that for every single unit?
I plan to make them have no collision, and no pathfinding. So hopefully it will be alright. But I’ll give this method a shot anyway and see what happens. Thanks
You can do this. I recommend labeling the AIs according to which team they are on, too. You can do this by adding all the units of the teams to respective tables, or tagging them with CollectionService. This will help to avoid looping through all AIs and only the enemies.