Block grid placement has an offset

I’m trying to place a block on the grid. The grid system works absolutely fine but its not going on the correct position.

Common Fix
I’ve tried adding and subtracting the placement position by the block size divided by 2, This works but the blocks will move away from the desired position.

Here’s what I’m doing…

-- This is the math I'm doing to  put it on a grid.

local grid_size = 2.4

function snap(x)
	return math.floor((x / grid_size) + 0.5) * grid_size
end

function snapVector(v)
	return Vector3.new(snap(v.X), snap(v.Y), snap(v.Z))
end
-- Applying the position to the placeholder block.

local mouse = player:GetMouse()

if mouse.Hit then
	viewBlock.Position = snapVector(mouse.Hit.Position)
end

Result

Under the block, above the green layer, is the marker i want the block to go to.

The result of using this code

1 Like

idk i watched a vid once and maybe its because you baseplate isnt a multiplication of your grid size

like if ur size is 3 then ur baseplate size should be a multiplication of 3 ex. 153

2 Likes

You should divide the Position by 2, to get the center position.

2 Likes

return math.floor((x / 2) + 0.5) * 2

1 Like

You don’t need the + 0.5.

It isn’t doing anything.

math.floor returns a whole number as far as I know.

Using a decimal for your grid size does seem to be causing an issue.

Not working…
Screenshot 2024-03-28 193435

The math.floor rounds off the value, for example if i wanted to round of to 60 and my value was 25 i would divide that by 60 giving me ~0.417. we then added 0.5 giving us 0.917 then flooring the number giving us 1 then multiplying it by our desired value which will give us 60 if our value was 72 these calculations would give us 120

if we did your method the rounding off would be completely ignored and offset the value.

Screenshot 2024-03-28 193623

That wouldnt work eg, if my vector was 4,2,8 i would return 2,1,4 not what i want!

I don’t think I understand… can you give me an example with rough code?

I have a simple block placing file you could look at if you want.

It works perfectly.

Yes i did mention that but its offset if bothering me because its not directly above the block i need a way to reduce its sensitivity because if i past the block by halfway it goes to the next grid

Try to remove +0.5 from math.floor()

Here is the simple block placement file:

Block.rbxl (50.8 KB)

that wont work, i talked about the calculations above

That would work but i dont feel like i agree with that

Will work. math.floor round number down. 1.01 = 1, 1.99 = 1. In your case, I see that your mouse is offsetted by half of block - 0.5. Try remove it, and test, it won’t take 2 hours.

Yes thats works, i just have to add an offset to it! thank you, its not want i clearly want but its fine for now!

1 Like

no math.floor or flooring in maths does not make the number round, it will lower the number until it becomes a whole number, for example: 1.67 - math.floor would make that number 1 instead of 2 because its flooring it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.