I’m trying to make a grid base placement system, each ‘cell’ is 12x12 studs and the building models’ sizes are dynamic.
So 12x12 studs, fitting perfectly in 1 cell. Or 12x24, where it needs 2 cells in width, and that is where my problem is. I can’t get my head over the allignment if the object is larger than 1 cell, where i could just center it to the grid cell.
I got the allignment correct in one of the axis with the larger model, as you can see in the video
(https://i.gyazo.com/2e00b5c3d20e8e5878e9157402e93d8d.mp4)
Here is my code; (the ‘P’ parts are for visualization)
local function PreviewBuildingPlace(Model)
if BuidlingPreviewClone then BuidlingPreviewClone:Destroy() end
if BuildingPreviewConnection then BuildingPreviewConnection:Disconnect() BuildingPreviewConnection = nil end
BuidlingPreviewClone = workspace[Model]:Clone()
local highlight = script.PlaceHighlight:Clone()
highlight.Parent = BuidlingPreviewClone
Mouse.TargetFilter = BuidlingPreviewClone
BuidlingPreviewClone.Parent = workspace
BuildingPreviewConnection = RunService.RenderStepped:Connect(function()
local MouseHit = Mouse.hit
local Increment = GridIncrement
local ObjectSize = BuidlingPreviewClone:GetExtentsSize()
local NearestX = math.floor(MouseHit.X / Increment) * Increment
local NearestZ = math.floor(MouseHit.Z / Increment) * Increment
local adjustedSizeX = math.floor(ObjectSize.X)
local adjustedSizeZ = math.floor(ObjectSize.Z)
local numGridX = math.ceil(adjustedSizeX / Increment)
local numGridZ = math.ceil(adjustedSizeZ / Increment)
print(adjustedSizeZ / Increment)
warn(numGridX, numGridZ)
local CenterX = NearestX + (Increment / 2 * numGridX)
local CenterZ = NearestZ + (Increment / 2 * numGridZ)
P4.Position = Vector3.new(CenterX, 1, CenterZ)
local ModelPivot = BuidlingPreviewClone:GetPivot()
local NewPosition = Vector3.new(
CenterX,
ObjectSize.Y / 2,
CenterZ
)
BuidlingPreviewClone:PivotTo(CFrame.new(NewPosition) * CFrame.Angles(0, math.rad(PreviewYRot), 0))
end)
end
All help is appreciated!