I can’t see any differences between your code and mine. This is certainly frustrating. Could it be the way I am calling the function? My full code is as below (you can ignore the second region):
using UnityEngine;
using System.Collections;
public class HexManager : MonoBehaviour {
#region HexInfo
public float HexUnitLength;
[HideInInspector] public Vector3[] HexVertices;
[HideInInspector] public Vector2[] HexUV;
[HideInInspector] public int[] HexTriangles;
public Texture HexTexture;
[HideInInspector] public GameObject HexTile;
void HexMeshSetup() {
float floorLevel = 0;
float HexSizeMult = HexUnitLength / Mathf.Sqrt(3);
HexVertices = new Vector3[] {
/*0*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(3+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(3+0.5)/6)))),
/*1*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(2+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(2+0.5)/6)))),
/*2*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(1+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(1+0.5)/6)))),
/*3*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(0+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(0+0.5)/6)))),
/*4*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(5+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(5+0.5)/6)))),
/*5*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(4+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(4+0.5)/6))))
};
HexTriangles = new int[] {
1,5,0,
1,4,5,
1,2,4,
2,3,4
};
HexUV = new Vector2[] {
new Vector2(0,0.25f),
new Vector2(0,0.75f),
new Vector2(0.5f,1),
new Vector2(1,0.75f),
new Vector2(1,0.25f),
new Vector2(0.5f,0),
};
HexTile = new GameObject("Single Hex Tile");
HexTile.transform.position = Vector3.zero;
HexTile.transform.rotation = Quaternion.identity;
HexTile.transform.localScale = Vector3.one;
MeshFilter HexMeshFilter = HexTile.AddComponent<MeshFilter>();
MeshRenderer HexMeshRenderer = HexTile.AddComponent<MeshRenderer>();
Mesh HexMesh = new Mesh();
HexMesh.vertices = HexVertices;
HexMesh.triangles = HexTriangles;
HexMesh.uv = HexUV;
HexMesh.RecalculateNormals();
HexMeshFilter.mesh = HexMesh;
HexMeshRenderer.material.mainTexture = HexTexture;
}
#endregion
#region GridGeneration
public int GridWidth;
public int GridHeight;
public Vector3 C aalcInitPos() {
// Position of the first tile placed will be the upper-left corner of the grid.
Vector3 InitPos;
InitPos = new Vector3(-HexUnitLength * GridWidth / 2f + HexUnitLength / 2,
0,
HexUnitLength * GridHeight / 2f - HexUnitLength / 2);
return InitPos;
}
public Vector3 CalcWorldCoord(Vector2 GridPos) {
Vector3 InitPos = CalcInitPos();
float RowOffset = 0;
if (GridPos.y % 2 != 0)
RowOffset = HexUnitLength / 2;
float x = InitPos.x + RowOffset + GridPos.x * HexUnitLength;
float z = InitPos.z - GridPos.y * HexUnitLength * 0.75f;
return new Vector3(x, 0, z);
}
void CreateGrid() {
//HexGrid will be a game object which is the parent of all the individual hex tiles
GameObject HexGrid = new GameObject("HexGrid");
for (float y = 0; y < GridHeight; y++) {
for (float x = 0; x < GridWidth; x++) {
Vector2 GridPos = new Vector2(x, y);
GameObject HexClone = (GameObject)Instantiate(HexTile);
HexClone.transform.position = CalcWorldCoord(GridPos);
HexClone.transform.parent = HexGrid.transform;
}
}
}
#endregion
#region Run
void start() {
HexMeshSetup();
CreateGrid();
}
#endregion
}