Hello! I’m currently working on a building game, and I’m trying to implement object snapping based on distance.
I’ve provided a reference video that shows sort of what I want:
I want to be able to place any block freely, but if the placing “preview” comes close enough to any other block they should snap together.
It sort of works, except that the forward axis doesn’t snap.
I should mention that I want to be able to stop the blocks from snapping if the camera moves far enough away.
Do a raycast from the camera to the center of the screen. If you hit a cube, simply position your preview cube at the next grid position in the direction of the raycast hit normal.
You can also use the overlapping box trick to check if any “free placed” block would be touching another block and simply disallow it. That seems to be what’s happening from the video you shared if the venter of the screen was not over an existing block. Correct me if I’m wrong there though.