Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Mesh triangles not aligning with vertices

Discussion in 'World Building' started by TheCopper, May 26, 2020.

  1. TheCopper

    TheCopper

    Joined:
    Mar 4, 2016
    Posts:
    5
    Hello, I am currently having an issue with my Mesh generation. Each mesh you see on screen should align with the gizmos you can see on the lower left of the screen. Each Mesh is an individual mesh as a component of each individual hex.

    upload_2020-5-27_1-6-31.png

    This is the code inside the HexObject.cs that generates the vertices and triangles. Apologies for the messy, lengthy lines, but each line simply creates the vertex correctly according to its owner's position.

    Code (CSharp):
    1.     public void CreateMesh()
    2.     {
    3.         mesh = new Mesh();
    4.         GetComponent<MeshFilter>().mesh = mesh;
    5.  
    6.         vertices[0] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2, 0, row * 1.5f);
    7.         vertices[1] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 + Mathf.Sqrt(3) / 4, 0, row * 1.5f + 0.75f);  //1
    8.         vertices[2] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 + Mathf.Sqrt(3) / 2, 0, row * 1.5f + 0.5f);  //2
    9.         vertices[3] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 + Mathf.Sqrt(3) / 2, 0, row * 1.5f + 0);  //3
    10.         vertices[4] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 + Mathf.Sqrt(3) / 2, 0, row * 1.5f - 0.5f);  //4
    11.         vertices[5] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 + Mathf.Sqrt(3) / 4, 0, row * 1.5f - 0.75f);  //5
    12.         vertices[6] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2, 0, -1f + row * 1.5f);  //6
    13.         vertices[7] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 - Mathf.Sqrt(3) / 4, 0, row * 1.5f - 0.75f);  //7    
    14.         vertices[8] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 - Mathf.Sqrt(3) / 2, 0, row * 1.5f - 0.5f);  //8      
    15.         vertices[9] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 - Mathf.Sqrt(3) / 2, 0, row * 1.5f + 0);  //9
    16.         vertices[10] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 - Mathf.Sqrt(3) / 2, 0, row * 1.5f + 0.5f);  //10
    17.         vertices[11] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2 - Mathf.Sqrt(3) / 4, 0, row * 1.5f + 0.75f);  //11
    18.         vertices[12] = new Vector3(column * Mathf.Sqrt(3) + row * Mathf.Sqrt(3) / 2, 0, 1f + row * 1.5f);  //12
    19.  
    20.         triangles[0] = 0;
    21.         triangles[1] = 12;
    22.         triangles[2] = 1;
    23.  
    24.         for (int i = 1; i <= 11; i++)
    25.         {
    26.             triangles[i * 3] = 0;
    27.             triangles[i * 3 + 1] = i;
    28.             triangles[i * 3 + 2] = i + 1;
    29.         }
    30.  
    31.         UpdateMesh();
    32.     }
    33.  
    34.     public void UpdateMesh()
    35.     {
    36.         mesh.Clear();
    37.  
    38.         mesh.vertices = vertices;
    39.  
    40.         mesh.triangles = triangles;
    41.  
    42.         mesh.RecalculateNormals();
    43.     }