Ray detecting multiple parts at once

Is it possible to have 1 ray detecting multiple parts at once? I cannot find any information about this and the default workspace-ray functions return only 1 part. Is there a hidden property that allows the ray to continue until the endpoint and collecting all of the parts that intersect with that ray? And if that is not possible can you explain me why this hasn’t been added?

This would be really usefull for me and it would save me from spawning alot of rays after eachother.

1 Like

There’s no built-in way of doing this. But you can achieve the same effect by raycasting repeatedly, adding each target to an ignore list before doing the next raycast. One implementation might look like so:

function findAllPartsOnRay(ray)
	local targets = {}
	repeat
		local target = game.Workspace:FindPartOnRayWithIgnoreList(ray, targets)
		if target then
			table.insert(targets, target)
		end
	until not target
	return targets
end

You can read up on the other two raycasting functions here and here. The second argument to FindPartOnRayWithIgnoreList is a table containing all the parts that you don’t want to “hit” with the ray.

13 Likes

I have something simular to this but then it creates a new ray every time. Never thought of reusing the ray. Thank you for clearing things up.

This still requires a loop tho and I prefer not to have one.
A feature request maybe?

It’d definitely be nice to have a built-in way of doing this, especially since it would probably run quite a bit faster when doing many of these sorts of raycasts.

This question has already been asked, and this was my response:

3 Likes