Raycast picking up odd results

Im trying to make a system that throws multiple raycasts in an arc and returns everything that was detected along them.

Trouble is, I’m getting odd results. The raycasts often dont detect parts they are passing straight through, and they do detect some parts that aren’t even close to the ray.

Here’s a recording of what is happening:
The rays stop well before they reach any of the rigs, and yet it is detecting them anyway.

95c1ef14e82d95e94565a6f5f1d9cacf

In this recording, multiple rays pass through the rig, but the rig is not detected
4d7aad000aae7790f24c39983ffbf28b

The function that visualizes the raycasts with a part accepts the ray itself as a parameter and places the part based on its origin and direction properties, so I’m certain that the visualization is accurately representing the rays being used in the function.

1 Like

Post the code (:

32 characters

1 Like

module containing the visualization function:

local module = function(ray, life)
	local origin = ray.Origin
	local dir = ray.Direction
	
	local CF = CFrame.new(origin, dir)
	local pos = (origin + dir)/2
	local len = (dir - origin).magnitude
	
	local part = Instance.new("Part")
	part.Size = Vector3.new(.1, .1, len)
	part.CFrame = CF
	part.Position = pos
	part.Anchored = true
	part.CanCollide = false
	part.Material = Enum.Material.ForceField
	
	part.Parent = game.Workspace.Effects
	game.Debris:AddItem(part, life or 1)
end

return module

Function that collects all parts along a raycast:

--returns all parts found on a ray
local function findAllPartsOnRay(ray, ignorelist)
	local targets = ignorelist or {}
	repeat
		local target = game.Workspace:FindPartOnRayWithIgnoreList(ray, targets)
		if target then
			table.insert(targets, target)
		end
	until not target
	return targets
end

Function that collects all parts along multiple raycasts, positioned in a radial order:

--detect for parts in a semicircle
local function RadialDetect(pos, angle, length)
	local iterations = 10
	local detected = {}
	angle = angle - 90
	
	for i = 1, iterations do 
		local start = CFrame.fromOrientation(0, math.rad(angle), 0)
		
		local ray = Ray.new(pos, pos + start.LookVector * length)
		detected = findAllPartsOnRay(ray, detected)
		
                --This is where the module function is called
		RayToPart(ray)
		
		angle = angle + 180/(iterations - 1)
	end
	
	return detected
end

It may be better to use a half-cylinder ShapeCast instead of shooting a bunch of rays.

1 Like

Dang, that actually seems like a way better solution. I will implement this instead. Still though, for the interest of this thread and my own curiosity, I would like to know why the rays are behaving this way. I’m tempted to say this is happening because this method of raycasting is deprecated and possibly unreliable, but in my experience that is never the case, and the error ends up being on my side.

I have revised my function to utilize ShapeCast:

--detect for parts in a semicircle
local function RadialDetect(pos, angle, length)
	local detected = {}
	
	local CF = CFrame.fromOrientation(0, angle, 0)
	CF = CF + pos
	
	SemiCollider.Size = Vector3.new(length, 1, length * 2)
	SemiCollider.CFrame = CF 
	
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	
	repeat
		local target = workspace:Shapecast(SemiCollider, Vector3.new(), params)
		if target then
			print(target)
			table.insert(detected, target)
			params:AddToFilter(target)
		end
	until not target
	
	return detected
end

where SemiCollider is a half cylindrical mesh. This does not appear to be detecting anything at all, I am not sure what I am doing wrong.