Finding Nearest Humanoid in a Target Radius

I am working on a detection system that requires the nearest humanoid to be found. However, I plan to support 100-150 player servers. This means efficiency/optimization is of the highest priority. To accomplish this, I’ve come up with the idea of only detecting objects in a circular radius. However, because of my lack of geometrical knowledge, I can’t think of the solution for this. So, calling all mathematicians:

Details:

  • I do NOT want to create a part object but rather a mathematical region used to determine if an object is inside that region.

If someone can assist me with the mathematics of this, I can complete the code with filters for humanoids.

4 Likes

I would probably use Player | Documentation - Roblox Creator Hub over Vector3 | Documentation - Roblox Creator Hub.

Well yes, that would help to determine the closest object. However, keep in mind, I need the radial calculations to determine if I should measure the distance of that object at all.

I retrieved the equation from a friend who isn’t affiliated with Roblox. For any other developers that need the equation, here is the code in it’s most efficient form:

-- p = Point you want to measure. c = Center point. s = Radius size.
local isObjInRad = function(p,c,s)
    if math.sqrt((p.x - c.x)^2 + (p.y - c.y)^2 + (p.z - c.z)^2) < s then
        return true;
    else
        return false;
    end
end
5 Likes

I would not say so.

local function isObjInRad(p,c,s)
     return math.sqrt((p.x - c.x)^2 + (p.y - c.y)^2 + (p.z - c.z)^2) < s
end

Also this is what magnitude does ? math.sqrt(x ^ 2 + y ^2 + z ^ 2)

(p - c).magnitude < s

I would test DistanceFromCharacter as it might even run on the C++ side of Roblox which could even be faster. But you should be doing your own tests to find out which is faster.

3 Likes