Placing items on different elevations

Currently how I place my items is based on part called ‘Base’ which is the PrimaryPart of the players plot area. This worked fine, until releasing having raised buildings, multiple floors, etc. Would not work


As you can see the chair is staying ‘connected’ to the Base (which has the grid like pattern on it.
What can I do so the player can only place items within the Base, but the items still move up and down depending on the floor? Another problem I’ve faced in the past with this stuff is that items move up the, so the item needs to always be on the ground.

Here’s what’s used to move the item every frame basically (to follow the mouse)

local renderStepped	
renderStepped = runService.RenderStepped:Connect(function()
	local cframe = placement(
		playersPlot.PrimaryPart, -- The Base part
		itemClone, 
		mouse.Hit.p, 
		var.Rotation
	)
	itemClone:SetPrimaryPartCFrame(cframe)
end)

Here’s the placement function

function placement(basePart, model, position, rotation)
	local cframe, size = baseSize(basePart)

	local modelSize = CFrame.fromEulerAnglesYXZ(0, rotation, 0)*model.PrimaryPart.Size
	
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

	local lpos = cframe:PointToObjectSpace(position)
	local size2 = (size - Vector2.new(modelSize.x, modelSize.z))/2

	local x = math.clamp(lpos.x, -size2.x, size2.x)
	
	local y = math.clamp(lpos.y,-size2.y, size2.y)

	local grid = var.Grid
	
	if grid > 0 then
		x = math.sign(x)*((math.abs(x) - math.abs(x)%grid) + (size2.x%grid))
		y = math.sign(y)*((math.abs(y) - math.abs(y)%grid) + (size2.y%grid))
	end
	
	return cframe*CFrame.new(x, y, -modelSize.y/2)*CFrame.Angles(-math.pi/2, rotation, 0)
end

Then calculations for the BaseSize

function baseSize(base)
	local canvasSize = base.Size

	local back = Vector3.new(0, -1, 0)
	local top = Vector3.new(0, 0, -1)
	local right = Vector3.new(-1, 0, 0)

	local cframe = base.CFrame*CFrame.fromMatrix(-back*canvasSize/2, right, top, back)
	
	local size = Vector2.new((canvasSize*right).magnitude, (canvasSize*top).magnitude)

	return cframe, size
end

So I’m guessing it would have something to do with the BaseSize function. Bare in mind, majority of this isn’t my code, I used this for the base and placement calculations and just integrated it into my own design

4 Likes

What happens when the Basepart is on another elevation?

The basepart can only be on one elevation. The thing I’m trying to do tho is have it so placement isnt so much based on the basepart, but just on parts within it

You could try to switch the primary part based on what the mouse.target is hovering on?

For keeping the items within the barriers: Isn’t there already a way to keep the items you want to place within the region of the primary part? Using the module you’ve linked earlier. (I may be wrong)

Ye, the items already do stay within the set base parts sizes. It’s just that they don’t move up or down depending on the height of other parts. They stay basically stuck to the floor

Did you try to change the primary part based on mouse.target?

I feel having to constantly change the primary part is too tedious for something like this. I’m sure there’s a simple solution.

You could try getting the object beneath it, and adding that objects Size.Y to the PrimaryPart.Position.Y? And if that makes it go too high, then do Size.Y / 2.

Been editing some numbers around,

return cframe*CFrame.new(x, y, -modelSize.Y/2)*CFrame.Angles(-math.pi/2, rotation, 0)

this line here

-modelSize.Y/2

is what determines the height of the model. Adding or negating number from it raises and lowers the model. So from here, how can I change this to make it so it’s

-modelSize.Y/2 -- to keep the model on the floor

but to make it move up and or down depending on the floor

Just for example, this is what happens if I do this

(-modelSize.Y/2) - 2)


As you can, the model hovers 2 studs above the grid floor

1 Like