Thank you! Checking it out right now.
You could do a magnitude check, and then do some math to see if a part is in a sphere.
Edit: Here is a code example:
for i, v in pairs(workspace:GetDescendants()) do if v:IsA("BasePart") then local magnitude = (v.Position - Sphere.Position).Magnitude if magnitude <= (Sphere.Size.X + Sphere.Size.Y + Sphere.Size.Z) / 3 then -- Boom. Part is in the sphere -- Do something... end end end
Have you tried using:
It can be used like this:
local Part = -- The sphere local Parts = Part:GetTouchingParts() if #Parts > 0 then for i,v in pairs(Parts) do print(v) end end
I would agree that magnitude is the best option for a spherical check.
I haven’t, but when I create a sphere part it is surrounded by the square highlight, which exceeds the physical sphere and was confused. Will the GetTouchingParts() include the parts that are also inside that highlight/selection or just within the physical sphere?
I’ll check it out, thank you.
The highlight is the bounding box, so it doesn’t affect it at all.
You can use magnitude to check since it’s a sphere.
Why is a magnitude check more preferred than the GetTouchingParts() function? It doesn’t look more efficient? Is there some kind of outside variable that may affect it?
Magnitude is going to be cheaper than using
GetTouchingParts because it doesn’t preform any intersection tests, all Magnitude is the distance from 2 vectors.
GetTouchingParts, because you might run into some rotation and size issues/complications with just using Magnitude alone (if you care about accuracy).
Well, it isnt preferred but its simpler, one downside to GetTouchingParts is that you cant use it on parts with can collide set to false.
Magnitude isn’t preferred, because then you would have to check lots of instances 10 times or more per second. Unless, of course, there are not many instances.
@FastAsFlash_Dev Yes, you can, just add a touched event:
local Part = -- The sphere with can collide set to false Part.Touched:Connect(function() end) local Parts = Part:GetTouchingParts() if #Parts > 0 then for i,v in pairs(Parts) do print(v) end end
You arent disconnecting the function which is expensive
I recommend this topic
Honestly, this is all great info to know. I’ll use magnitude as I’m already afraid the amount of tiles in the game may be costly.
What are you talking about? Every can collide true part has a touched event. In games there might be thousands of them! Please don’t blabber out of nowhere(sorry for being rude). The reason I didn’t disconnect it is because they might have to run the function the whole game.
@WarInEuropeRBLX If there are many tiles, using the
GetTouchingParts() method would be better, unless you are only using magnitude occasionally. Imagine looping through 100 parts every second. But it won’t cause much of a difference.
How are you going to reference the part later and disconnect it? It’s not recommended to leave the signal running.
And also since op might use a lot of parts for it, having so many running signals is ridiculous
Have you read my post?
Basically every part has a touched event, adding a few more does nothing.
True, but he might need it the whole time. The only time you disconnect it is if you were to never use it again.
The sphere will be created once and will run only to detect 19 particular parts within itself. It wouldn’t take a lot of resources, I think, but it would be best for the game, practice, and familiarity.
The sphere will afterwards be deleted after the detection, as it would no longer be used.
I am talking about parts with cancollided on false.
Haha, no. The script activity might increase beyond 3% which is a bad practise.
My point is, it’s just a wastage of resources.
Woah, that’s going to be laggy.
You should first reduce the number of parts you need to search over with an efficient method such as Region3, then do the magnitude checks.
You can do this by using a Region3 which encases the sphere. It’s dimensions will be
2r x 2r x 2r where r is the radius of the sphere where the region3 has the same centre position as the sphere’s centre.
Finally, after you get all parts in that region, magnitude test which parts lie within a distance r from the centre of the sphere.