# Struggle with Mathf.Ceil and Mathf.Floor

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.

Ieyasu

I think you need to handle each of the six faces separately. You can use the normal to tell which face you hit, and then do the right thing. For example if normal = (0,0,1), then you you’ve hit the +Z face, so the coordinates of the cube are Floor(hit.x), Floor(hit.y), Round(hit.z)-1. But if you’ve hit the -Z face, then it’d be just Floor(hit.x), Floor(hit.y), Round(hit.z), because the hit point is already on the correct coordinate (the lower range of the cube, if I understand your setup correctly).

1 Like

Hello, and yes you get it, and I think that you nail it!

That’s a nice idea to use Round only for the face with the float approximation.

I just try it, it work like a charm.
Thanks a lot!

1 Like