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.
-
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.
-
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.
-
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.