Find nearby objects?

I’m prototyping an idea for an RTS game. I’d like to have it so that units will automatically attack other nearby units. However, I’m having trouble figuring out an efficient way to detect the other nearby units.

A basic example of how to do this would be to simply find the distance between the current unit and all other units. However, doing this would get very costly performance-wise, especially when there’s hundreds of units on the field performing this check every second or so.

So my question is, what would the best, most performance efficient way of going about checking what objects (parts) are nearby? As I’ve said, optimization is key. While there may not be hundreds of units ingame at all times, it’s good to plan for the worst. If someone could help me figure this out, it would be greatly appreciated.

Thanks!

I would probably try with the magnitude checks first. It is not as intensive as you seem to think it is. The movement & unit handling will be the real efficiency killer if not done properly.

(this is anecdotal, but my thinking works like this: i have never run into problems running a magnitude check every .01 second and i use it fairly often. i dont know its actual performance impact but i dont know that it is as devastating as you believe it to be)

By the way, I’ve borrowed the code from the drooling zombie model.

local searchTarget 

searchTarget = Vector3.new(model.HumanoidRootPart.Position.X + xoff,model.HumanoidRootPart.Position.Y,model.HumanoidRootPart.Position.Z + zoff)

if searchtarget then
model.Humanoid:MoveTo(model, searchTarget)
end
1 Like

Alright, I’ll try this tomorrow and post the results.

1 Like