I am trying to make a grid snapping system that is similiar to Lumber Tycoon 2’s building system, but the issue is that the grid just doesn’t snap the models correctly when they are rotated at 90 degree angles, as you can see below:
This is what i expect it to look like:
As you can see in the expected result, all models are snapped to the 1-stud grid.
I have tried rounding the position of the model, however the model’s size is 2x4x1, which is what I think is causing this problem to happen. When I tried it with a 2x2x2 model, it worked perfectly fine.
I have also tried searching on the DevForum but I wasn’t able to find any solutions for this type of problem.
--[[
The getBoundingBox() function is for returning a bounding box with
no rotation (which is what :GetBoundingBox() can't do), I got it
from this topic:
https://devforum.roblox.com/t/216581/8
]]
function roundToMultiple(x, mult)
return math.floor((x / mult) + 0.5) * mult
end
...
-- Placing the object:
elseif input.KeyCode == Enum.KeyCode.E then
Storage.PlaceBlueprint:FireServer(place_cf, is_placing)
...
-- Moving the preview:
while is_placing do
local dt = task.wait()
local mouse = UserInputService:GetMouseLocation()
local ray = camera:ViewportPointToRay(mouse.X, mouse.Y)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {plot}
local result = workspace:Raycast(ray.Origin, ray.Direction.Unit * 1024, params)
if result then
-- This 'bounds' model is basically a clone of the 'preview' variable but it is not shown, it is only for calculating the bounding box.
bounds:PivotTo(orientation)
local _, size = getBoundingBox(bounds:GetDescendants())
local offset = size * result.Normal / 2
local position = Vector3.new(roundToMultiple(result.Position.X, 1), roundToMultiple(result.Position.Y, 1), roundToMultiple(result.Position.Z, 1))
position += offset
local target = CFrame.new(position) * orientation
place_cf = target
local cf = pivot:Lerp(target, dt * 15)
preview:PivotTo(cf)
if (target.Position - cf.Position).Magnitude > 50 then
cf = target
preview:PivotTo(target)
end
pivot = cf
end
end