Grid-style placement offsetting incorrectly

So I have a fairly bare-bones script that allows me to place a block in a grid on every render step. Now a problem I have been encountering is that the block, when placed, is offset half a stud even though I coded it to be a 1 stud grid.

What I’m doing is that I’m rounding the X, Y, and Z position of the mouse ray’s hit and setting the block’s position to that.

I’ve tried using math.ceil(x), but to no avail. I’ve also tried doing math.floor(x + 0.5) which also resulted in the same offset as I did with math.ceil(x).

math.ceil(x) code:

rs.RenderStepped:Connect(function()
    local b2 = block:Clone()
    b2.Parent = bf
    
    local posX = math.ceil(mou.hit.p.X)
    local posY = math.ceil(mou.hit.p.Y)
    local posZ = math.ceil(mou.hit.p.Z)
    
    b2.Position = Vector3.new(posX, posY, posZ)
end)

math.floor(x + 0.5) code:

rs.RenderStepped:Connect(function()
	local b2 = block:Clone()
	b2.Parent = bf
	
	local posX = math.floor(mou.hit.p.X + 0.5)
	local posY = math.floor(mou.hit.p.Y + 0.5)
	local posZ = math.floor(mou.hit.p.Z + 0.5)
	
	b2.Position = Vector3.new(posX, posY, posZ)
end)

How it looks: https://gyazo.com/21ec1fbaff0b108122736f63cfad3878

Please help if you can.

If your offsets are off by only .5 for each stud, try applying the transformation after your floor operation.

local posX = math.floor(mou.hit.p.X) + 0.5
local posY = math.floor(mou.hit.p.Y) + 0.5
local posZ = math.floor(mou.hit.p.Z) + 0.5
1 Like

Oh! That makes much more sense now that I think about it. Thanks for the help!

1 Like