I’m working on a hex grid/ map and when the map’s rotation is 0,0,0 it is working nicely.
I can hover over or click on a tile and it identifies it correctly.
The little helper spheres denote the mesh’s vertices:
However, when i apply a rotation to the map (pre-compile), something odd happens:
Note how the cylinder is correctly aligned with the helper spheres, however the drawn mesh is now at an angle.
Clicking on a tile will now move the cylinder to the according position on the grid of helper spheres.
The assigned coordinates for the vertices and spheres are exactly the same (lines 5, 8, 13, 17):
void DoTheVertices () {
foreach (KeyValuePair<Vector2, Tile> element in allTiles) {
foreach (Vector3 edge in element.Value.GetEdges ()) {
vertices [arrayCounter] = edge;
arrayCounter++;
}
vertices [arrayCounter] = element.Value.GetCenterPosition ();
arrayCounter++;
bool testCubes = true;
if (testCubes) {
Instantiate(testCube2, element.Value.GetCenterPosition (), Quaternion.identity);
Vector3[] edges = element.Value.GetEdges ();
foreach (Vector3 edge in edges) {
Instantiate(testCube, edge, Quaternion.identity);
}
}
}
}
I’m building the mesh like so:
void CreateMap () {
FillDictionary ();
vertices = new Vector3[7 * numberOfTiles];
triangles = new int[18 * numberOfTiles];
normals = new Vector3[7 * numberOfTiles];
uvs = new Vector2[7 * numberOfTiles];
centerVertexIndexToTile = new Tile[numberOfTiles];
DoTheTrianglesUVSAndTilesProperties ();
DoTheNormals ();
DoTheVertices ();
Mesh mesh = new Mesh ();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uvs;
MeshFilter meshFilter = GetComponent<MeshFilter>();
//meshFilter.transform.rotation = transform.rotation;
meshFilter.mesh = mesh;
MeshCollider meshCollider = GetComponent<MeshCollider>();
//meshCollider.transform.rotation = transform.rotation;
meshCollider.convex = false; // this is needed to get the triangleIndex in Update. Dunno why...
meshCollider.sharedMesh = mesh;
}
I hope someone can help me out here. I’ve been at this bug for hours and have no idea what is going on. I tried to set the orientation of the parts of the mesh explicitely, but that didnt do anything.