Snapping to grid wall object

I recently made a post (very recently) but now I want to snap it to a grid but it just makes half of the object out of the wall (and I want part A in the middle X of part B)

But the grid just makes it go out the half of the grid

This It’s my current code

    local snappedVector = target.CFrame:VectorToObjectSpace(vector)
    return target.CFrame:VectorToWorldSpace(
        Vector3.new(
            math.round(
                (snappedVector.X) / grid
            ) * grid,
            math.round(snappedVector.Y / grid) * grid,
            math.round(
                (snappedVector.Z) / grid
            ) * grid
        )
    )
1 Like

Refrain from replying to this post if you won’t post a useful formula to solve this.

1 Like

Try this

local function snap(vector)
	local newVector = Vector3.new(
		math.floor((vector.X / BuildController.Settings.GridSnap)) * BuildController.Settings.GridSnap,
		math.floor((vector.y / BuildController.Settings.GridSnap)) * BuildController.Settings.GridSnap,
		math.floor((vector.Z / BuildController.Settings.GridSnap)) * BuildController.Settings.GridSnap
	)

	return newVector
end

where BuildController.Settings.GridSnap is your grid in studs. 0.5 or 1 for example

1 Like

I already tried that with more maths but doesn’t seem to work with wall objects or the way I’m trying to achieve.

It snaps really well for me, I also have walls and stuff. My build items are only the same size, thickness and stuff. For models i don’t really care about placements. From your image it seems like the item you try to build has a different size, is that intentional?

Trying to set the part into the middle of the target part (I did it) but doesn’t work with grid.

Weird, cool that you got it working tho :+1:

I did not get anything, I only achieved to snap the part in the middle but it doesn’t work with grid

I dont know what you mean, snap without grid sounds vague to me

I mean this
image
If you look carefully, you will notice that the part it’s in the middle, not in the sides, but with grid, that breaks

I see that you managed to put the part nicly in the middle, and I assume you mean “grid” as in

I cannot help you there, the code you showed is for snapping in the world space and has nothing to do with the grid shown on the terrain

Edit, I myself have to implement this yet into my game, so i dunno really

Oh if thats the only problem then just multiply the end cframe that you are setting it to times CFrame.new(0, part.SIze.X / 2, 0) – (you will definitely have to adjust the axes here)