Problems with casting ray

https://gyazo.com/d81b34c7f769e7ece7cdae271a327e53
When the player hits the boundary along the “y-axis”, raycasting along the “y-axis” works perfectly fine.
However, when the player hits the boundary along the “x-axis”, additional tiles are shown visible even though I only want all tiles within 5 tiles of the player to be shown.

	ShowTile(self.Hex)
	for _, Direction in pairs(self.DirectionTable) do
		local FilterTiles = {self.Hex}
		--local FilterTiles2 = {self.Hex}
		
		--raycast along x tiles
		for i=5, 1, -1 do	
			local RayResult = workspace:Raycast(self.HexOrigin, Direction, self.RayParams)
			if RayResult then
				Result = RayResult.Instance
				if Result.Parent.Name == "Tile" then
					table.insert(FilterTiles, Result)
					self.RayParams.FilterDescendantsInstances = FilterTiles
					ShowTile(Result)	
				end
			end
			-- Raycast along y tiles
			for _, Direction2 in pairs(DirectionTable2) do
				for i=5, 1, -1 do	
					local RayResult = workspace:Raycast(Result.Position, Direction2, self.RayParams)
					if RayResult then
						local Result = RayResult.Instance
						if Result.Parent.Name == "Tile" then
							table.insert(FilterTiles, Result)
							self.RayParams.FilterDescendantsInstances = FilterTiles
							ShowTile(Result)					
						end
					end
				end
			end
		end

Is it because you have the second for i=5, 1, -1 do loop inside the first for i=5, 1, -1 do loop?

The first for loop cast the ray five times along the “x-axis”, each time it adds the hit tile to its ignore list, thus making 5 tiles visible.
The next for loop casts a ray five times for each “x-axis” tile along the “y-axis”, doing the same thing.

In this way, it makes all tiles visible in a 5x5 grid relative to the player.

Found a solution, the problem was when the player hits the boundary on the “x-axis”, it still casts a ray along the “y-axis”, even though there are no tiles. To solve this I expanded the if RayResult then so that a ray is only cast along the “y-axis” when there are more tiles along the “x-axis”.

1 Like