Incorporate surface normal with position of part

I’m trying to incorporate the surface normal returned from a ray, so a part will actually go along the sides of blocks, but I don’t know how.

-- Get the mouses target
local function GetMouseTarget()
	local CursorPosition = UserInputService:GetMouseLocation()
	local CameraRay = Camera:ViewportPointToRay(CursorPosition.X, CursorPosition.Y, 0)
	local DrawnRay = Ray.new(Camera.CFrame.Position,(CameraRay.Direction * 1000))
	
	return workspace:FindPartOnRayWithWhitelist(DrawnRay, {PlayersIsland.Blocks})
end

-- Snap to grid
local function Snap(position)
	local x = math.floor(position.X / GridLock + 0.5) * GridLock
	local y = (math.floor(position.Y / GridLock + 0.5) * GridLock) + (GridLock / 2) -- Add 1/2 of the GridLock to adjust for ground position
	local z = math.floor(position.Z / GridLock + 0.5) * GridLock
	
	return Vector3.new(x, y, z)
end

-- Move item
local function MoveModel()
	if not CurrentItem then return end
	
	-- Get mouse location
	local Part, Position, Normal = GetMouseTarget()
	
	if not Part then return end
	
	-- Snap returned position to grid
	local RoundedPos = Snap(Position)
	
	CurrentItem:SetPrimaryPartCFrame(CFrame.new(RoundedPos))
end

This code currently does this, and it works on 2 of the sides of the ground


But other sides, it doesn’t, either the block disappears, or is placed on top


Both times my mouse is on the side of the ground.

I tried

CurrentItem:SetPrimaryPartCFrame(CFrame.new(RoundedPos, Normal))

But that ended up giving the part like a 45 degree rotation

3 Likes

Use this :

CFrame.LookAt(PartPos, PartPos + Normal)