New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

Best way to detect parts entering/leaving a radius? (LOD system)

Asked by 6 months ago

So my question is this: What's the best way to detect parts that enter and leave a radius around the player?

I plan to use this (if we can figure it out) to make a LOD system. If a model/part is inside a set radius, it looks "normal", if it leaves that radius it gets swapped out with a lower quality version.

At the moment I've setup a script that detects parts in a radius, but not when they leave it. The code! (a script inside StarterCharacterScripts)

while wait(1)do
    local Point1 =script.Parent.HumanoidRootPart.Position,10,10)
    local Point2 =script.Parent.HumanoidRootPart.Position,2,10)
    local Region,Point1)
    for _,Part in pairs(workspace:FindPartsInRegion3(Region,script.Parent,math.huge))do

If possible, I'd like to have 2-3 layers of outward detection. e.g. If a tree is within 10 studs, it's fully rendered, if it's only within 20 studs, its animations are paused and it's swapped with a slightly less detailed version, and finally, if it's within 30 studs it's swapped out with a 2D image of the tree.


In terms of a radius, you can use Magnitude, Region3 creates a square area BlackOrange3343 2658 — 6mo
The code I posted uses Region3 and dose detect when parts are in the radius. What I'm looking to do is get it to return parts when they leave aswell as enter the radius. mbramblet 80 — 6mo

1 answer

Log in to vote
Answered by 6 months ago
Edited 6 months ago

Hay, great question, I think I can help!!!

So if I understand correctly you need to detect when player enters radius around part/model.

I think here, radius actually is just threshold of distance. Other way of formulating "radius" is following: Level of distance between player and part/model, that once crossed, triggers defined behavior on parts/models.

The way of doing this is rather simple, you just need to figure out distance between player and targeted part.

Best way to do this is to use magnitude(vector function which returns length of vector3 value) and have if statements like this:

if (model.position - player.position).magnitude < 10 then -- have it fully rendered elseif (model.position - player.position).magnitude < 20 then -- have its animations paused, swapped with a slightly less detailed version elseif (model.position - player.position).magnitude < 30 then -- have it swapped out with a 2D image of the tree. end

Edit:To work with ALL parts within the radius you can use region3. You can detect parts inside region and then loop through all of them. But remember, this region3 should change(follow player) depending on players position

Note:This will need some optimisation if player has to encounter many parts in radius.

This can do almost exactly what I need! Thank you. The only problem is that I don't have just one part to use as the position of "model.position". It needs to work with ALL parts within the radius. mbramblet 80 — 6mo
I edited the answer, see "Edit" DemaxDev 35 — 6mo
Ah yes, Thank you again. mbramblet 80 — 6mo

Answer this question