So, i created a really simple script (suck at programming) to generate a 3D mesh of an hexagon, and when i run it, it looks like the mesh gets flattened out, so instead of getting a 3D mesh, i get a 2D mesh of the hexagon. The fun part is that my script actually works, it generate a 3D mesh (At leats, that’s what my mesh filter say; And my vertices are in the right position, i’ve checked hundreds of times), but for some reason it gets flattened on the y plane (Vertically). It looks like i can’t extend my mesh’s triagles further from the vector z of my trasform position. My project is set to 2D, but i tried to make a new 3D project to see if that was the problem. It wasn’t. What did i get wrong? Here’s some images, probably you’ll understand better.
Here’s what i get:
Here’s what the mesh should look like(i’ve got 20 triangles and 12 vertices, the right number: Maybe the normals are a bit messed up):
Here’s the script:
private void generateMesh(Material standard)
{
MeshRenderer hexRend = gameObject.AddComponent<MeshRenderer>();
MeshFilter hexFilt = gameObject.AddComponent<MeshFilter>();
Mesh hexMesh = hexFilt.mesh;
MeshCollider hexCol = gameObject.AddComponent<MeshCollider>();
hexCol.convex = true;
Rigidbody meshRig = gameObject.AddComponent<Rigidbody>();
meshRig.isKinematic = true;
hexRend.material = standardMaterial;
hexMesh.Clear(); hexMesh.ClearBlendShapes();
float scale = transform.localScale.x;
float xxx = scale + (scale / 1.4f);
Vector3[] corners = new Vector3[12];
// lato alto dell'esagono
corners[1] = new Vector3(scale, 0);
corners[2] = new Vector3(scale + (scale / 2), xxx / 2);
corners[3] = new Vector3(scale, xxx);
corners[4] = new Vector3(0, xxx);
corners[5] = new Vector3(-(scale / 2), xxx / 2);
for (int x = 6; x < corners.Length; x++)
{
corners[x] = corners[x - 6];
corners[x] += new Vector3(0, 0, scale);
}
hexMesh.vertices = corners;
hexMesh.triangles = new int[]
{
0, 4, 5,
0, 3, 4,
0, 1, 3,
1, 2, 3,
0, 6, 1,
1, 6, 7,
1, 2, 7,
2, 7, 8,
2, 3, 8,
3, 8, 9,
3, 9, 10,
3, 4, 10,
4, 10, 11,
4, 5, 11,
0, 5, 6,
5, 6, 11,
6, 10, 11,
6, 9, 10,
6, 7, 9,
7, 8, 9
};
hexMesh.RecalculateNormals();
}
I’ve tried to re-write the script completely so the hexagon get generated on the z plane, instead of the y plane, but it get flattened out on the z plane (So horizontally, instead of vertically; My hexagon got generated, but 2D on the z plane): I’ve tried to create a new unity project, to see if maybe i messed something up: I’ve tried to rotate the vertices, to they face the camera: I’ve tried to attach the script to a primitive 3D object, to see if maybe was the trasform of a new empty object (just because the project is set to 2D) to flatten the mesh: But nothing worked. I don’t know what to do anymore. Please… Help. Thanks in advance.
btw i’m not an english native.