Hi guys!

I’m trying to learn unity with Voxels and I have some problems.

I’m having fun with mesh, I add vertices, triangles, and can make nice cubes made “by hand”. Nothing extraordinary but it’s nice to learn this stuff.

My struggle is, that I want to determine wich cube the cursor of my mouse is pointing. Considering that I refer to my voxels (cubes) by the smallest vertice on x, y and z.

All my cubes are parts of an unique mesh, they aren’t independant.

Now imagine two cubes, in (0, 0, 0) to (1, 1, 1) et the second one at the right of the first one: from (1, 0, 0) to (2, 1, 1).

What I do:

I cast a ray on the mouse click to determine witch cube is hit.

I got (when a face is hit of course) a nice Vector3 with floats values depending of the position of my cursor.

I draw a debugLine to see the normal of my hit, it work well I can see where my mouse is poiting on my mesh.

What I intend to do here is to simplify this Vector3 to determine the origin of the Voxel (cube) hit by my raycast.

To do so, I create un new fresh Vector3, and then add for x the Mathf.Floor value of my hit position on the collider, and so on for the y and the z.

I expect to get a nice Vector3 with smooth integer “floored” values, witch I plan to substract my normal vector (When it is positive), and then, I hoped to get the origin vertex of my cube.

So if I hit a face on (0.5432, 0.1121, 1), I would get a (0, 0, 1) Vector3 as the “smallest” vertex of this face.

Than, I would substract the normal vector, here (0, 0, 1) to my “floored” hit, and finaly get a (0, 0, 1)-(0, 0, 1) = (0, 0, 0) Vertex, the origin of the cube hit by the raycast.

Actually, it dosn’t work at all.

I have tried with the Ceil insteed of the Floor method, but I have the same result:

It seems that the hit on the collider give approximative float values, and sometime the float number insteed of being 1 is randomly slightly above or under the expected integer value.

So, the floor or the ceil method give random results, with sometimes the good value, and sometime +1 or -1.

It’s really annoying.

So, maybe I have not the good method to determine the origin of my Voxel with a raycast hit, maybe you have an idea ti improve mine?

I’m really sorry about my crapy English, as a French it’s not really easy to explain 3D technical stuff.

Thanks for your help,

Ieyasu