'Snap to grid' system is offset slightly

Hello Devforum! I have a pretty simple request, I think.

I know that snap to grid systems are very common, so I have tried looking on the devforum. However, every system i’ve found (that I understand enough how to use) also has the same problem. And when I search for people who have a similar problem to mine, the solution is heavily based on their provided code, and I don’t understand their code nor how they reached a solution. I’m not sure how to search for a ‘snap to grid’ system that also fixes my very particular problem, so I thought i’d just ask.

  1. What do you want to achieve?
    I have a part that follows the mouse position. I want my part to snap to a grid similar to how it is in studio. I also want my system to support changing the grid size/part size
A video of what I want / how it works in studio.

  1. What is the issue?
    My current system does snap to the grid. There’s no issue with a grid of size 1 or 2. But when my grid size is 4, it’s offset slightly by 2 studs. I want the grid to match the grid texture on the floor - like it does in studio.
My scripts

Snap function

local function Snap(position,gridSize)
	local x = math.round(position.x/gridSize)*gridSize
	local z = math.round(position.z/gridSize)*gridSize
	return Vector3.new(x,position.y,z)
end

Runservice Connection
(The previewPart is just a part with the size of (4,1,4), for testing purposes)

	runServiceConnection = RunService.Heartbeat:Connect(function()
		local raycastResult = RaycastToMouse(raycastParams)
		if not raycastResult then
			return
		end
		
		previewPart.Position = Snap(raycastResult.Position, 4)
	end)
A video of how my current system works.

  1. What solutions have you tried so far?
    I’ve tried changing the part’s PivotOffset, but it had no effect.
    I’ve also tried just adding an offset like so: previewPart.Position = Snap(raycastResult.Position,GRID_SIZE) + Vector3.new(2,0,2) , and it does fix the problem. But it also makes the part follow the mouse really strangely, and I would rather it didn’t do that.
A video of it following the mouse strangely

Any help is much appreciated, thank you.

1 Like

Try with :

local x = math.floor(position.x/gridSize)*gridSize
local z = math.floor(position.z/gridSize)*gridSize

If that doesn´t work: I´m going to think about a possible solution to fits to your problem :slight_smile:

1 Like

Hi!
Using math.floor instead of math.round fixed the wierd mouse movements when i used the offset! It works perfectly now. Something so simple, but I never considered, since just using math.floor without the offset doesnt work, I had to use both. :person_facepalming:t4:
Thank you so much!

EDIT: And this made it work for every different size grid, too ! not just a size 4 grid.

local gridSize = 8
local gridOffset = Vector3.new(gridSize/2, 0, gridSize/2)
previewPart.Position = Snap(raycastResult.Position,previewPart.Size, gridSize) + gridOffset

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