How to snap to grid a different sized block in a block building system?

  1. What do you want to achieve?
    I want to make my block building system snap to grid a vertically tall block (which is 4x8x4 studs).

  2. What is the issue?
    image
    The block appears to be floating half a block (2 studs).

local pos = hit.Position + hit.Normal * offset

pos = Vector3.new(
	math.floor((pos.X + offset.X) / 4) * 4,
	math.floor((pos.Y + offset.Y) / 4) * 4,
	math.floor((pos.Z + offset.Z) / 4) * 4
)

local pivot = CFrame.new(pos) * rot
ghostBlock:PivotTo(pivot)

This is the code which pushes the block in the normal and snaps it onto the grid.
hit is the RaycastResult from workspace:Raycast() method.
offset is the block’s size divided by 2.

  1. What solutions have you tried so far?
    I have tried tried changing the offsets but none of them work.
    I also tried searching for answers but there aren’t any.
    I searched through Stamper Tool by Maximum_ADHD but I can’t wrap my head around it.

I’m trying to make a door in my block game but I can’t make different sized blocks work properly. When the blocks are 4x4x4, their centers are at 2, 2, 2. But when it is 4x8x4, it is at 2, 4, 2 which makes it float 2 studs. When I change the offset to subtract 2, it also changes the 4x4x4 positioning, making it go underground 2 studs. Is there a method to position any multiple of 4 sized block without using conditions? Or do I need to use conditions?

1 Like
math.floor((pos.X + offset.X) / 4) + .5 * 4,
math.floor((pos.Y + offset.Y) / 4) + .5 * 4,
math.floor((pos.Z + offset.Z) / 4) + .5 * 4
1 Like

image

It now seems to be on half of the distance it should be on.
It is because 0.5 (.5) * 4 = 2, which results in half of the distance being covered.
It also appears to now go below the ground.

ik mine was wrong but your code isn’t right, here’s a demonstration math.floor(pos.X / Grid + .5) * Grid

image

pos = Vector3.new(
	math.floor(pos.X / 4 + .5) * 4,
	math.floor(pos.Y / 4 + .5) * 4,
	math.floor(pos.Z / 4 + .5) * 4
)

Produces the same output as original.
pos / 4 + 0.5 =
pos / 4 + 2/4 =
(pos + 2) / 4 =
(pos + offset) / 4

Why are you doing it on the Y Axis, If you want the wood to be positioned correctly on the baseplate or a specific part then you need to use raycasting.

You need to offset it down or up by half a gridsize, because of this:


When you resize a part, it resizes from the center. So your 8 stud part is now offset by exactly half a gridsize (2 studs)

An easier method would be to set the pivotoffset of the parts to one of their corners. That’d avoid this problem.