Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Editor & General Support' started by Ikanow, May 22, 2024.

  1. Ikanow

    Ikanow

    Joined:
    May 9, 2024
    Posts:
    2
    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.

    Code (CSharp):
    1. // get the mesh data
    2. var meshData = tiles[i].GetComponent<MeshFilter>().sharedMesh;
    3.  
    4. for (int j = 0; j < meshData.vertices.Length; j++)
    5. {
    6.     Vector3 vertex = meshData.vertices[j];
    7.     Vector3 normal = meshData.normals[j];
    8.     Debug.Log(vertex.x);
    9.     if (vertex.x == 1) { posX.Add(new Vector2(vertex.z, vertex.y)); posXnormals.Add(new Vector2(normal.z, normal.y));  }
    10.     if (vertex.x == -1) { negX.Add(new Vector2(vertex.z, vertex.y)); negXnormals.Add(new Vector2(normal.z, normal.y)); }
    11.     if (vertex.y == 1) { posY.Add(new Vector2(vertex.x, vertex.z)); posYnormals.Add(new Vector2(normal.x, normal.z));  }
    12.     if (vertex.y == -1) { negY.Add(new Vector2(vertex.x, vertex.z)); negYnormals.Add(new Vector2(normal.x, normal.z)); }
    13.     if (vertex.z == 1) { posZ.Add(new Vector2(vertex.x, vertex.y)); posZnormals.Add(new Vector2(normal.x, normal.y));  }
    14.     if (vertex.z == -1) { negZ.Add(new Vector2(vertex.x, vertex.y)); negZnormals.Add(new Vector2(normal.x, normal.y)); }
    15. }
    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:

    empty arrays.PNG

    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:

    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:

    default.PNG

    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.
     

    Attached Files:

  2. dreeca

    dreeca

    Joined:
    Jun 2, 2017
    Posts:
    3
    CodeSmile likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    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.
     
  4. Ikanow

    Ikanow

    Joined:
    May 9, 2024
    Posts:
    2
    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.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    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.