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 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.
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
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.
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.