Parts Clipping Through Boundaries

Hello Developers!

I’m just going to drive straight into my problem. I’m working on a building system with boundaries. But, when a part is placed on the side of said boundaries, half of it “clips” outside of the boundary.

As seen in this photo

I am wondering how to fix this problem.

My Code

This code handles the movement of parts. This is also a LocalScript.

local MovingPart
local origin

Handles:GetPropertyChangedSignal("Adornee"):Connect(function()
	MovingPart = Handles.Adornee
	origin = MovingPart.CFrame
end)

Handles.MouseDrag:Connect(function(a, b)
	
	if a.Value == 5 then
		MovingPart.CFrame = origin + MovingPart.CFrame.LookVector * b
	elseif a.Value == 2 then
		MovingPart.CFrame = origin + MovingPart.CFrame.LookVector * -b
	elseif a.Value == 1 then
		MovingPart.CFrame = origin + MovingPart.CFrame.UpVector * b
	elseif a.Value == 4 then
		MovingPart.CFrame = origin + MovingPart.CFrame.UpVector * -b
	elseif a.Value == 0 then
		MovingPart.CFrame = origin + MovingPart.CFrame.RightVector * b
	elseif a.Value == 3 then
		MovingPart.CFrame = origin + MovingPart.CFrame.RightVector * -b
	end
	
	local posX = math.clamp(MovingPart.Position.X, BuildingArea.Position.X - BuildingArea.Size.X/2, BuildingArea.Position.X + BuildingArea.Size.X/2)
	local posY = math.clamp(MovingPart.Position.Y, BuildingArea.Position.Y - BuildingArea.Size.Y/2, BuildingArea.Position.Y + BuildingArea.Size.Y/2)
	local posZ = math.clamp(MovingPart.Position.Z, BuildingArea.Position.Z - BuildingArea.Size.Z/2, BuildingArea.Position.Z + BuildingArea.Size.Z/2)

	MovingPart.Position = Vector3.new(posX, posY, posZ)
	
end)

Handles.MouseButton1Up:Connect(function()
	
	origin = MovingPart.CFrame
	
end)
1 Like

Just bumping this topic. It’s dryer then a desert in here…

At a glance it looks like you’re not taking into account the size of the block. If they are ending up 1/2 way out, subtract 1/2 the part width maybe. I’m assume you also made the boundary based off where the block end up and didn’t just make a boundary, now looking to add blocks.

1 Like

You should check whether the part placement is valid before placing it there. You can do this by checking whether there are parts or terrain inside your part.

To check which parts are inside a part, you could use WorldRoot:GetPartsInPart().

1 Like

From testing, it looks like my code is moving the block from the dead center. So if I subtract half the block, the other half will still stick out.

Do you know if there are anyways to take in account the full size of the block?

local block = – Your block

local blockSize = block.Size
local xSize, ySize, zSize = blockSize.X, blockSize.Y, blockSize.Z
print(“X size of the block:”, xSize)
print(“Y size of the block:”, ySize)
print(“Z size of the block:”, zSize)

or more like …
This is x,y and z. Probably don’t need all 3 in this case to be /2.

local block = – your block
local position = – the position it’s going to

local blockSize = block.Size
local xOffset = blockSize.X / 2
local yOffset = blockSize.Y / 2
local zOffset = blockSize.Z / 2

block.Position = position - Vector3.new(xOffset, yOffset, zOffset)

I’m not sure what all you’re doing. But the blocks should fit inside your border flush to start with by whatever amount of movement. You may want to change your boarder a bit.

You said it’s starting dead center, so it is already placed at a 1/2 way point vs size.

1 Like

Wouldn’t such an offset create even more problems, though? Checking whether a part placement is valid rather than adjusting it seems like the better solution to me, which is why that’s what I always do when creating placement systems.

Plus, you’re only accounting for one boundary, what about other boundaries in different directions? If you try to place it on a boundary facing the other way, the part would be way inside the boundary.

I guess that would depend on the shape of the part and the math to move it.

“You said it’s starting dead center, so it is already placed at a 1/2 way point vs size.”
This probably had more to do with it than anything …

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.