Voxel Building is not working

So pretty much, I am trying to place a block on the walls of another, but the mouse.Hit function isn’t rounding right since it goes a little into the block. Like instead of -4.5 (the wall of the block) its -4.437 and since it snaps to a 3x3 grid, it snaps to -3 instead of -6. How do I fix this? I tried raycasting but it did the same thing.

The issue comes from mouse.Hit being perfectly between two grid positions. You have to nudge it towards the camera to ensure that it rounds the block closest to you
-4.437 versus -4.5 is quite a big difference though, your block might not be aligned properly

I have two solutions to this issue,

First one:

local MouseHit = mouse.Hit * CFrame.new(0,0,0.1) -- mouse.Hit points in the direction it's looking, so towards negative z, so moving it by position z will bring it towards you
-- CFrame multiplication is relative, the position change is relative to the orientation of mouse.Hit
-- If it instead goes into the block, either change the sign (-0.1) or make that value bigger

This solution works well, but with cube voxels, you can build diagonally to other voxels, which is quite odd

Second solution:

local Position = RaycastResult.Position + RaycastResult.Normal/10

This solution doesn’t have the problem of the first one, it changes the Position using the Normal of the hit surface. The normal points outwards from the face of the mesh/part that was hit
You can change the /10 to adjust by how much position of moved out. The normal always has a length of 1, so /10 means a change of 0.1 studs. This can be used to make it so you can build against the slanted edge of a wedge, sphere, etc. In your case, it might not change anything

One negative about this one is that you kind of need to use Raycasts and RaycastResults, to get the normal vector. It isn’t available using the mouse object

thank you so much. your ONE line of code (first solution) had fixed my ENTIRE problem

1 Like

also, because of this, i can now place blocks on corners which is ALSO something i wanted

1 Like