Depending on what youre doing I can think of two major options for what you can do:
- Loop through all of the relavent parts in the workspace and use a formula to determine if theyre in the cylinder
- Make multiple raycasts distributed through the cylinder
Raycasting Solution
I’ll firstly explain how you might do the second option.
I made a bit of sample code using the sunflower seed arrangement, the final product of which looks something like this:
The code of which looks like this:
function dist.sunflower(max_n,radius)
--max_n is the number of parts you want within the radius
--radius is how big the cylinder is (this is the CENTER TO EDGE distance, not edge to edge so be careful)
for n = 0,max_n do
local c = radius/math.sqrt(max_n)
local theta = (2*math.pi)/2.61803398875*n
local r = c*math.sqrt(n)
local p = Instance.new("Part")
p.CFrame = workspace.cpart.CFrame*CFrame.new(math.cos(theta)*r,math.sin(theta)*r,0)
p.Size = Vector3.new(0.1,0.1,0.1)
p.Parent = workspace
end
end
This could be easily changed to work for raycasting, changing the bottom portion where it creates a part to something along the lines of:
local distanceToCast = 50 --youd probably want to make this value the distance between the camera and mouse
local cframe = camera.CFrame*CFrame.new(math.cos(theta)*r,math.sin(theta)*r,0)
local raycastParams = RaycastParams.new()
--whatever raycastparams you need for this
local res = workspace:Raycast(cframe.Position,cframe.LookVector*distanceToCast)
--process ray and detect for parts
Formula Based Solution
For the formula based solution, you might, instead of using rays, want to opt for a different solution with more accuracy but higher performance usage
If youre just looking if the CENTER of the part is within the cylinder
I would most likely use the Dot Product to solve for the parallel distance to the center of a cylinder, using the formula:
(SIDE NOTE) You would loop through every part you need to check in workspace and run this code for each of them
local distanceToCast = 50 --youd definitely want to make this value the distance between the camera and mouse
local projectedDistance = camera.CFrame.LookVector:Dot(targetPart.Position-camera.CFrame.Position)
if projectedDistance > 0 and projectedDistance < distanceToCast then
local parallelDistance = (camera.CFrame.LookVector * projectedDistance - targetPart.Position).Magnitude
if parallelDistance < RADIUS_OF_CYLINDER then
--do stuff (if this if passes the center of the part is within the thing)
end
end
If youre looking if the part is inside at all
The formula is similar but adds in some roblox aided raycasting
(SIDE NOTE) You would loop through every part you need to check in workspace and run this code for each of them
local distanceToCast = 50 --youd definitely want to make this value the distance between the camera and mouse
local projectedDistance = camera.CFrame.LookVector:Dot(targetPart.Position-camera.CFrame.Position)
if projectedDistance > 0 and projectedDistance< distanceToCast then
local res = workspace:Raycast(camera.CFrame.LookVector * projectedDistance,camera.CFrame.LookVector * RADIUS_OF_CYLINDER)
--process the raycast
if res.Instance == targetPart then --you might just want to whitelist targetPart
--if this passes then ANY part of the target part is within the cylinder
end
end