Raycasting with Callback

It would be great if we could get a raycasting method with a callback :]

The callback would return a boolean:
If true then the search will end and that part will be returned.
If false then the search will continue and the part will be ignored.

Example usage

local function IsPartValid(part)
  local isValid = part.CanCollide
  print("Checking:", part, isValid)
  return part.CanCollide
end

print("Starting search...")
local part, position = workspace:FindPartOnRayWithCallback(bulletPathRay, isPartValid)
print("Returned:", part)

Example output

> Starting search...
> Checking: LightingEffect false
> Checking: LightingEffect false
> Checking: Particle false
> Checking: Wall true
> Returned: Wall

Also, it would be nice if we could get a Ray.new overload that accepted 6 numbers rather than 2 Vector3’s, for performance reasons:

Ray.new(originX, originY, originZ, directionX, directionY, directionZ)

This would make raycasting much more powerful than it already is :imagine:

1 Like

FindPartOnRay yields, so why would you need a callback?

Not really a callback, more a selector.

But then again, if you can afford doing multiple raycasts, you can do something like this:

local nRay = Ray.new
local function RayCast(origin, direction, ignore)
	return workspace:FindPartOnRayWithIgnoreList(nRay(origin, direction), ignore)
end
function RayGetPart(origin, direction, selector)
	local ignore = { }
	local p = RayCast(origin, direction, ignore)
	while p do
		if not selector(p) then
			ignore[#ignore+1] = p
		else
			break
		end
	end
	return p
end

It would be nice but an entire method dedicated to it would be overkill imo. Just add a callback to the end of the current methods.

I feel like that would make the ignore list redundant