Vertecies are slightly different when viewed in code, making comparisons difficult

Hello, I am currently trying to implement an algorithm to place 3D tiles on a grid, and to do that I would like to calculate some adjacency constraints. I try to get each vertex which is on the face of my 2 by 2 by 2 large tiles by checking if the x, y or z value is equal to 1 or -1, and then store them in a list for later use. However, when I try to get these vertecies, they are often moved slighty.

// get the mesh data
var meshData = tiles[i].GetComponent<MeshFilter>().sharedMesh;

for (int j = 0; j < meshData.vertices.Length; j++)
{
    Vector3 vertex = meshData.vertices[j];
    Vector3 normal = meshData.normals[j];
    Debug.Log(vertex.x);
    if (vertex.x == 1) { posX.Add(new Vector2(vertex.z, vertex.y)); posXnormals.Add(new Vector2(normal.z, normal.y));  }
    if (vertex.x == -1) { negX.Add(new Vector2(vertex.z, vertex.y)); negXnormals.Add(new Vector2(normal.z, normal.y)); }
    if (vertex.y == 1) { posY.Add(new Vector2(vertex.x, vertex.z)); posYnormals.Add(new Vector2(normal.x, normal.z));  }
    if (vertex.y == -1) { negY.Add(new Vector2(vertex.x, vertex.z)); negYnormals.Add(new Vector2(normal.x, normal.z)); }
    if (vertex.z == 1) { posZ.Add(new Vector2(vertex.x, vertex.y)); posZnormals.Add(new Vector2(normal.x, normal.y));  }
    if (vertex.z == -1) { negZ.Add(new Vector2(vertex.x, vertex.y)); negZnormals.Add(new Vector2(normal.x, normal.y)); }
}

When debugging, I can see that the mesh I put in contains only the vertecies that it had when I exported the .fbx file from blender:

However, once the loop is through, the values are not asigned correctly. For example, vertecies [6] and [7] both have an x value of 1, so the condition if (vertex.x == 1) should have triggered, but both according to the debug menue and my results, that doesn’t happen:

The posX array, which is supposed to contain the vertecies where x == 1, is empty. When I try to debug using Debug.Log(vertex.x); I get weird results, in which the vertecies are often moved slightly:

9849030--1417788--output log meshdata.PNG

This also happens with other values, not just the x value. In other words, vertecies are being moved at the 7th or 8th place after the comma and I don’t know why. I am importing fbx files, with these settings:

These are the default settings in unity, with the exception of enabaling read/ write.

Any pointers as to what I am doing wrong would be greatly appreciated.

Hi,
You should never do equal comparisons with floats, they are not accurate.
You can look into how floats work if you want more details.

If you really must, you can try using:

1 Like

For logging, round float values to a specific number of decimals for better comparison.

For calculation, see above. Float values inherently carry with them a “margin of error” so to speak.

The higher the value the less accurate the decimal representation will be. This is why you get odd issues large distance away from origin, starting with about 5,000 units and definitely noticeable past 10-20k units. See Minecraft long-travel videos for some of the effects.

Do these float errors already occur on a number 1.0 or -1.0?! I didn’t know that, I thought it was only an issue for when you use very small or very large numbers, but as it stands, I am only using numbers which are 1, 0 or -1 and no others.

They always occur. Perhaps you noticed that sometimes, particularly with rotation, when you enter a value like 0.3 it might actually print 0.29999999 or something.