Raycasting from within a part

I’m creating a grid system which is composed of a group of cells. Each cell has an “Occupied” property, however it only sometimes works. The idea of it was that it would skip cells that would appear inside of parts, however it turns out that raycasting will not detect parts it’s inside of. This is my current code:

local Cell = Cells[GUID];
	
local origin = Cell.Position;
local direction = CFrame.new(Cell.Position).lookVector.Unit;
	
Cell.Occupied = workspace:Raycast(origin, direction, RaycastParams.new()) ~= nil;

I’m currently thinking of having rays which origins are farther from the cell, however then it’d only detect parts of certain sizes, and ignore the rest.

1 Like

Instead, consider using a Region3 check. That will tell if the cell is occupied in 3D space better than a raycast will.

1 Like

Everything works now with this code:

local Cell = Cells[GUID];
	
local origin = Cell.Position;
local mag = CFrame.new(Cell.Position).lookVector.Unit.Magnitude;
local offset = Vector3.new(mag, mag, mag);
	
Cell.Occupied = #(workspace
	:FindPartsInRegion3(Region3.new(origin - offset, origin + offset), nil, 5) or {}) == 0;