Checking collision
OK, let's backtrack here for a second. Let's stop talking about colliders, raycasting, GameObject
s, Transform
s etc. It's overkill. VOXELS != UNITY
. Voxels existed long before Unity, and will long after.
What is a voxel space (as in Minecraft)? Fundamentally, it is this:
Voxel[,,] voxels = new Voxel[VOLUME_WIDTH, VOLUME_HEIGHT, VOLUME_DEPTH];
...a 3D array. THAT array is the basis on which you perform logical checks, NOT colliders.
Yes, you will need to raycast where the user is looking. There are algorithms like 3D DDA that can do this incredibly cheaply without the need for fancy, costly Unity physics-based raycasting.
Alternative placement
Having gotten that out of the way, let's look at alternative placement. You can set whatever criteria you like. I would suggest limiting it, step by step. For example, the first constraint I would put on the location the user is trying to place at is
We can move back, forward, left and right - but we MUST place at
the same height.
Already there we've simplified the problem greatly. Now deciding whether we can place for example a 2x2x2 cube into a space looks at the next step: Given a space where the user is pointing at a given height Y at position XZ, does X+1 (or X-1) and Z+1 (or Z-1) accommodate a 2x2 shape. We are now working with planar logic, which is to say working on a flat plane. If there is no 2x2 flat plane in the immediate region (and by plane, I mean area with all the same Y) then we need to look elsewhere nearby, or give the user no alternatives at all.
Another crucial thing is where the ray strikes a surface. If you hit a ground surface (i.e. planar in XZ, such as what you may stand on) cool, let the above apply. However if there is a wall or cliff right in front of you, clearly you cannot place there. You now need to determine if the space directly below that is available for placement. So,
If we are looking at a vertical wall, we must check the placement area directly
below it, that is, the first horizontal section below that vertical
face, for potential placement.
Gradually, by introducing these rules one by one (and in particular order) you will end up with the behaviour for placement that you desire.