Issue With Snapping System


I’m currently having issues trying to make a grid system for my game, I’ve looked through numerous posts and asked for help all over the place but I’ve been unable to get any help. I’ve added the code i’m using to snap and show the item hologram. Any help will be heavily appreciated.

function snapToGrid(pos, increment)
	return Vector3.new(math.round(pos.X / increment) * increment, math.round(pos.Y / increment) * increment, math.round(pos.Z / increment) * increment)
end

mouse.TargetFilter = CurrentModel
local mousePos = mouse.Hit.Position
local target = result.Instance
if not target then return end
					
local surface, hitbox = mouse.TargetSurface, CurrentModel.Hitbox
local targetCFrame, targetSize = target.CFrame, target.Size
						
local hitboxSize = hitbox.Size
if shared.Rotation % 180 ~= 0 then hitboxSize = Vector3.new(hitboxSize.Z, hitboxSize.Y, hitboxSize.X) end

local snapped = snapToGrid(result.Position, 1)
local offset = result.Normal * (hitboxSize / 2)
local surfacePos = snapped + offset

local newPos
if CurrentlySelected == "Decorations" then -- decorations only have rotation
	newPos = CFrame.new(surfacePos) * CFrame.Angles(0, math.rad(shared.Rotation), 0)
else -- use tilt
	newPos = CFrame.new(surfacePos) * CFrame.Angles(math.rad(shared.Tilt), math.rad(shared.Rotation), 0)
end
					
CurrentModel:PivotTo(newPos)
1 Like

What’s wrong with it? Is it over shooting and not staying within the grid/increments specified?

it’s not staying within the grid, in the image the block is 0.5 studs off of the block.