Hello,
I am making a building game for fun. Placing blocks on the ground works perfectly, but placing blocks on the side of other blocks has a problem.
Placing works normally in the positive X and Z direction but not in the negative X and Z direction. This results in the block being placed going inside the block instead of on the side (visual representation is below). There is no issue with placement in the Y direction. I know it's a problem with the rounding but I can't quite figure it out.
Positive X and Z: https://i.gyazo.com/49d548f5ae0479192d6b822cd0c32df3.gif?_ga=2.209657489.2076798991.1542575956-1841802524.1538965118
Negative X and Z: https://i.gyazo.com/7e1218e29642a10ffde2793077cd4d7a.gif?_ga=2.239001219.2076798991.1542575956-1841802524.1538965118
This is the section of code that rounds the X value so it aligns with my placemend grid. The same code is used to round the Z value:
local factor = 4; --factor aligns position with placement grid position = mouse.Hit.p; --raw position of block local X = position.X; --raw X position local tep = X/factor; --raw X position aligned with grid local d, tepd = 0; tepd, d = math.modf(tep); if d >= 0.5 then --rounds X accordingly X = (math.ceil(tep))*factor; else X = (math.floor(tep))*factor; end
But wait! There's more!
After some experimenting I found that if you switch the math.ceil() and math.floor() functions on lines nine and eleven you can place blocks on the side in the negative X and Z directions but not in the positive X and Z directions. The code after the switch looks like this:
--all of this is unchanged if d >= 0.5 then --rounds X accordingly X = (math.floor(tep))*factor; --switched else X = (math.ceil(tep))*factor; end
I'm assuming there is a way to have it set up such that it works in all directions along the X axis and Z axis. But am I going about it the wrong way?
Any feedback is appreciated!
Depending on which side of the target part your mouse is on, you have to either round down or round up. If you only round down (math.floor), the placement is biased towards the negative X and Z directions, and if you only round up (math.ceil), it's biased towards the positive X and Z directions.