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 +Vector3.new(10,10,10) local Point2 =script.Parent.HumanoidRootPart.Position -Vector3.new(10,2,10) local Region =Region3.new(Point2,Point1) for _,Part in pairs(workspace:FindPartsInRegion3(Region,script.Parent,math.huge))do Part.Color =Color3.new(1,0,0) end end
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.
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.