Grid snapping into other blocks

I am making a grid placing system for my circuitry game, but there is a flaw in my code for the blocks are snapping into each other on certain sides. Has anyone else experienced this? Please assist me


My code:

function raycast(parts)
	local params=RaycastParams.new()
	params.FilterDescendantsInstances=parts
	params.FilterType=Enum.RaycastFilterType.Include
	local m=workspace.CurrentCamera:ViewportPointToRay(mouse.X,mouse.Y+game.GuiService.TopbarInset.Height)
	return workspace:Raycast(m.Origin,m.Direction*1000,params)
end

local pos=raycast({workspace.Blocks:GetDescendants(),workspace.roblox:GetDescendants()})
if not pos then return end
pos=pos.Position:Ceil()-Vector3.one*.5

The bottom chunk of code is the issue

Add a little bit of the RaycastResult normal to the position before you snap it.

1 Like

Thank you for the response! I will try this tomorrow and tell you how it goes.

It looks like you are only subtracting the Vector3, but wouldn’t that just place it to the negative side of the surface the mouse is on.
Also, are you using math.round or math.ceil for the placement? Round would work better because it rounds up or down based on the mouse position, where ceil would always go to the highest point (example 32.1 using ceil is 33, but using round would be 32)

There are a lot of other solved posts about making grid snapping or building game placement. I’m pretty sure most of them are using the mouse position instead of a raycast, then just placing the blocks on a rounded grid type setup.

1 Like

That’s exactly what I thought too! I was just experimenting with :Ceil(), but it miraculously works. If it ain’t broke don’t fix :person_shrugging:

By the way, this works. Thank you so much!

1 Like