I have a basic voxel engine up and running but now I want to be able to use raycasts to add and delete blocks.
The problem I have is when I use the raycast it selects the block according to the raycastHit.point. This works from above but when clicking on either side it tends not to choose the block I clicked on but rather the on on either side.
Has anyone got an easy method for clicking on a block ?
and to select the block next to the clicked face just add hit.normal to the blockVector
Edit: Now I’m getting a new problem. When using the above code I get a number for each axis (x, y, z) and on each side it can sometimes take the number down 1 editing the block its facing. e.g. clicking the top block should have the axis (x, 4, z) but randomly it rounds the 4 down to 3. How would I get around this even though the face is perfectly level with 4? I am using Mathf.Floor to pull the number down to a steady level, since I am using slanted blocks.
The problem may be that the hit point doesn’t come out exactly equal to 4, having some variation either way, and Floor amplifies the variation. You could try adding a small amount, e.g. 0.05, before calling Floor.