How would I make this drag/move item cframe function clamp to edges? It is important to me. Thanks

local itemheight = itemselected:GetExtentsSize().Y/2
			local itemlength = itemselected:GetExtentsSize().Z/2
			local itemwidth = itemselected:GetExtentsSize().X/2
			con = game["Run Service"].Heartbeat:Connect(function()-- may need debounce later on
				if not itemselected then return end
				local rc = workspace:Raycast(cc.CFrame.Position, (mouse.Hit.Position - cc.CFrame.Position).Unit * 35, rp )
				if rc then
					local itempivot = itemselected:GetPivot()
					local goalcf = CFrame.new(rc.Position + Vector3.new(0, 1.8 * itemheight +.5,0)) * CFrame.Angles(0,math.pi/2 * rot,0)
					
					local localrp = RaycastParams.new()
					localrp.FilterDescendantsInstances = {itemselected}					
					local function hasBounds(model)
						local bounds = model:GetExtentsSize()
						local boundingBox = model:GetBoundingBox()
						local modelCFrame = model:GetPivot()

						local directions = {
							modelCFrame.LookVector,      -- Front
							-modelCFrame.LookVector,     -- Back
							-modelCFrame.RightVector,    -- Left
							modelCFrame.RightVector      -- Right
						}
						-- Adjust the starting point of the raycast to the edge of the bounding box
						local raycastStartOffsets = {
							Vector3.new(0, 0, bounds.z / 2),           -- Front
							Vector3.new(0, 0, -bounds.z / 2),          -- Back
							Vector3.new(-bounds.x / 2, 0, 0),          -- Left
							Vector3.new(bounds.x / 2, 0, 0)            -- Right
						}

						for i, direction in ipairs(directions) do
							local raycastStart = goalcf.Position + modelCFrame:VectorToWorldSpace(raycastStartOffsets[i])
							local raycastResult = workspace:Raycast(raycastStart, direction, localrp )
							if raycastResult then
								print(raycastResult.Instance:GetFullName())
								return true
							end
						end
						return false
					end
					
						
					if not hasBounds(itemselected) then
						itemselected:PivotTo( itempivot:Lerp(goalcf, .08) )
					end
				end	
				if release then
					print("Item category ",itemcategory, "object ",itemselected)
					game.ReplicatedStorage.RemoteEvent:FireServer("PlaceObject", itemcategory, itemselected.Name, itemselected:GetPivot(), myfolder)
					
					itemselected.Parent = origparent
					origparent = nil
					
					itemselected = nil
					
					
				end
			end)

I want it so that when HasBounds is true then I want the model to be placed exactly where it is making contact with. If I move the mouse any further in then the object refuses to go through the wall it is raycasting or making contact with.

Help? I really just want it to clamp