# Is there a way to get all of the parts around a raycast with a radius? (region3 around ray?)

I am currently working on a system which creates a hitbox for every single part with a specific size.
It currently works by getting the parts via region3 around mouse.hit.Position, the issue with this is that it doesn’t grab the parts between mouse.hit.Position and the camera its position.

How can i get all of the parts between the Camera its position and mouse.hit.Position with a radius?

This will be run every step so it has to be efficient.

Can you clarify what you mean by all the parts between the cameras position and the mouses position with a radius?

Do you mean something like this?

Where every part within the green cylinder would be returned

1 Like

You could do like

``````t=(-o+r)/d
``````

Then plug it into the parametric ray equation and if t is positive and is greater than the projection of our objects in our scene and is less than the distance of them then they are within our path (make sure to apply bounds so its a ray and not a line!)

Yes this is what i need.
A region 3 wouldn’t work as it cant grab rotated parts.

Depending on what youre doing I can think of two major options for what you can do:

1. Loop through all of the relavent parts in the workspace and use a formula to determine if theyre in the cylinder
2. 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 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
--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``````
1 Like

I need to use it to detect where the blocks are.
In this case, the grey half transparent blocks dont show up when the mouse is really far away.
I need the grey parts to show up at each part which could be around the line from the mouse hit and the camera.

I dont think rays are the way to go for this situation.

I think your best bet if you dont want to raycast (if Im understanding what youre asking) would be the dot product based center based solution I have listed second in the above post

Id say the relatively small number of gray parts makes it not too much of a problem to use that method

1 Like

Is there a way to use this with Region3withwhitelist?
I would need to figure out how to make the region3 work from camerapos to mousepos at all angles.

You could look into EgoMoose’s Rotated Region 3 Module, however similar behavior could be achieved (even with a box shape) with all three methods with varying degrees of difficulty