How to raycast outward within a square shape?

Hi! I’m looking for how I’d evenly distribute rays outward within a square pyrimid shape, like this:

image

1 Like

You can try this

local center = Vector3.new(0, 5, 0)
local size = 5
local rays = {}

for x = -size, size do
  for y = -size, size do
    local direction = Vector3.new(x, 0, y)
    table.insert(rays, Ray.new(center, direction.unit * size))
  end
end

for _, ray in ipairs(rays) do
  local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {}, false, false)
  if hit then
    print(hit.Name)
  end
end

This would make the square pyramid by casting rays from the center in all directions, up to a certain size .

Thank you, but unfortunately this isn’t what I was looking for and that uses outdated code.

I realized I was overcomplicating it, and solved my problem by creating a grid and raycasting to each point

local GridSpacing = 5
local GridSize = 30
local Distance = 200


function VisualizeRaycast(Origin, Destination, RemoveTime)
	local Distance = (Origin - Destination).Magnitude
	local Part = Instance.new("Part", workspace)
	Part.Anchored = true
	Part.CanCollide = false
	Part.CanQuery = false
	Part.Size = Vector3.new(0.15, 0.15, Distance)
	Part.CFrame = CFrame.lookAt(Origin, Destination)*CFrame.new(0, 0, -Distance/2)
	Part.Color = Color3.fromRGB(255, 17, 17)
	Part.Material = Enum.Material.Brick -- so it's easy to see depth
	game.Debris:AddItem(Part, RemoveTime)
end

for gridX = -GridSize / GridSpacing, GridSize / GridSpacing do
	for gridZ = -GridSize / GridSpacing, GridSize / GridSpacing do
		
		local direction = Vector3.new(gridX * GridSpacing, -Distance, gridZ * GridSpacing)
		
		local result = workspace:Raycast(origin, direction)
		
		local position = result and result.Position
		
		if position then
			VisualizeRaycast(origin, position, 10000)
		end
		
	end
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.