How can I raycast in every direction from a position?

Really good attempt, there’s just a few tweaks to some negatives and things you needed. I also cleaned up the if statement stuff but your method also would work.

local function EvenlyDistributeVectorsInCone(n, angle, axis)
	if n == 0 then return {} end
	if n == 1 then return { axis } end
	local cosAngle = math.cos(angle)
	local vecs = table.create(n)
	local phi = math.pi * (3 - math.sqrt(5))

	for i = 0, n - 1 do
		local a = i / (n - 1)
		local z = -1 + (1-cosAngle)*a
		local radius = math.sqrt(1 - z * z)

		local theta = phi * i

		local vec = Vector3.new(math.cos(theta) * radius,math.sin(theta) * radius,z)
		
		if axis.Z > 0.9999 then
			vecs[i+1] = -vec
			continue
		elseif axis.Z < -0.9999 then
			vecs[i+1] = vec
			continue
		end
		
		local orth = -Vector3.zAxis:Cross(axis)
		local rot = math.acos(axis:Dot(-Vector3.zAxis))
		vec = CFrame.fromAxisAngle(orth, rot) * vec	

		vecs[i+1] = vec
	end

	return vecs
end
2 Likes