How to make preview be at edge?

I’m currently working on a game inspired by islands and bedwars, and in the bedwars placement system you can place your mouse on the edge of a block without touching a face of the block and it’ll place a preview there. Example:

my code:

local function renderPreview(blockTemplate)
	local mouseLocation = UserInputService:GetMouseLocation()
	local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, castParams)

	if cast and preview and placementValidator.IsWithinMaxDistance(player, cast.Position) then
		for _, v in pairs(preview:GetChildren()) do
			if v:IsA("Texture") then
				v.Transparency = 0.7
			end
		end
		local snappedPosition = placementValidator.SnapToGrid(cast.Position, preview.PrimaryPart.Size)
		local normalCF = CFrame.lookAlong(cast.Position, cast.Normal)
		local relativeSnapped = normalCF:PointToObjectSpace(snappedPosition)
		local xVector = normalCF:VectorToWorldSpace(Vector3.xAxis * -math.sign(relativeSnapped.X))
		local yVector = normalCF:VectorToWorldSpace(Vector3.yAxis * -math.sign(relativeSnapped.Y))

		local cf = CFrame.fromMatrix(snappedPosition, xVector, yVector, cast.Normal)
		preview.PrimaryPart.Position = cf:PointToWorldSpace(blockTemplate.PrimaryPart.Size / 2)
	else
		if preview then
			for _, v in pairs(preview:GetChildren()) do
				if v:IsA("Texture") then
					v.Transparency = 1
				end
			end
		end
	end
end

bump because no replys yetbump because no replys yetbump because no replys yetbump because no replys yet

the way i’d do it would be like:

  • if the mouse isn’t pointing at any target, project that to a point on the player’s current ground level
  • if there’s nothing there, and there is an adjacent block in the mouse’s grid position in the direction towards the player, then attempt to place there

How would I add that?

extra characters

You could use a block cast to get the nearest connection to the look direction. Compare all hit objects and take the smallest distance.
Once you have that position (The grid placement of the hit block) project a fake block with a highlight object to the grids hit location plus the direction from hit (The offset of the hit from the center of the beam)

Thats how I would go about doing it

I do not know how to do that, sorry.

Thats really the only way I can think to do it. Id recommend looking into raycasting as a whole.

A block cast is just a ray with dimentions. Use it to get edges close the the center of the cast.
The item closest to the center of the cast should be the one you are aiming at.