Blockcast not consistently detecting parts and terrain

I am working on a building system for a survival game where you can only place objects on parts or terrain. I am using :BlockCast() because :GetPartsInPart() and .Touched don’t work with terrain. However, I am having issues with this system because it is having issues detecting anything, whether it is a basepart or terrain. It seems to be based on position and rotation of the hitbox. Whenever the hitbox is touching something, it should always detect it and the hitbox should turn green. I think the problem is that the :BlockCast() is not always casting or it’s in the wrong position but I don’t know how to fix it. I have also tried sphere casting but it’s giving me the same problem.

Here is video demonstrating my problem (when the box flashes red its not detecting anything)

Here is the code to the script for creating the hitbox for detection (local script):

local function ObjectPreview()
	local pos = mouseRaycast()
	if not destroyed then
		
		mouse.TargetFilter = object
		Snap(pos) -- function for grid snapping
		object:SetPrimaryPartCFrame(CFrame.new(PosX,PosY,PosZ)*CFrame.Angles(0,math.rad(rotationY),0))
		
		local rayparams = RaycastParams.new()
		rayparams.FilterType = Enum.RaycastFilterType.Include
		rayparams.FilterDescendantsInstances = {game.Workspace}
		
		local blockcast = game.Workspace:Blockcast(hitbox.CFrame,hitbox.Size,hitbox.CFrame.LookVector,rayparams)
		
		if blockcast then
			canplace = true
		else
			canplace = false
		end
		
		--for the object preview
		hitbox.CanCollide = false
		for i, Aobject in pairs(previewmodel:GetChildren()) do
			Aobject.Transparency = 0.5
			Aobject.CanCollide = false
		end
		
		--for the outline color of the object (shown in video)
		if canplace then
			selectionBox.Color3 = Color3.new(0,1,0)
			selectionBox.SurfaceColor3 = Color3.new(0,2,0)
		else
			selectionBox.Color3 = Color3.new(1,0,0)
			selectionBox.SurfaceColor3 = Color3.new(2,0,0)
		end
		
	end
end
1 Like

Here, instead of doing hitbox.CFrame.LookVector, try doing -Vector3.xAxis * 5.

1 Like

I tried your code out and it works more consistently, but sometimes it still won’t detect the terrain. Maybe it has to do something with how terrain cells work?

local function generateTerrain(x,z,yPos,material)
		terrain:FillBlock(CFrame.new((x*Voxelsize)-offsetX, yPos, (z*Voxelsize)-offsetZ), Vector3.new(Voxelsize, Voxelsize*10, Voxelsize), material)
	end

Does anyone have a solution yet?

I’m not entirely 100% sure on how terrain voxels work, but I believe a little bit of terrain will protrude out from the actual data voxels. I would try just adding half a stud of size downwards and see if that helps.

If you look at the video, the hitbox should be going about 0.5 - 1 stud into the ground. Even when I forced into the ground (about 2 studs), it would still not detect anything.

Blockcast, any shapecast, and raycasts in general does not detect the object it was immediately spawned in.

image
Source: Introducing Shapecasts

Never found a solution to this problem (at least using blockcast). I made a new world generation script using parts and I used :GetPartInPart() to detect if you can place objects.