Hi everyone. For the past week I’ve been trying to create a script that is attached to an empty game object that creates a gird of hexs in the shape of a larger hex. The code below is just the code to create one row of hexs. For some reason whenever I make the number of hexs larger than one the triangles or vertices get all kinds of screwed up. I’m not exactly sure what I’m doing wrong.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class MeshCreator : MonoBehaviour
{
public float radiusval;
public float xval;
public float yval;
public int numHexs;
// Use this for initialization
void Start ()
{
createHex (xval, yval, radiusval);
}
// Update is called once per frame
void Update ()
{
}
void createHex (float _x, float _y, float _radius)
{
//Variables for calculations
float r2, x1, x2, y1, y2;
float x = _x;
float y = _y;
float radius = _radius;
//Arrays for vertices, normals, and traingles. Using the *2 to allow for large grids of hexs
Vector3[] vertices = new Vector3 [(6 * numHexs) * 2];
Vector3[] normals = new Vector3[(6 * numHexs) * 2];
int [] triangles = new int [(12 * numHexs) * 2];
int i;
int j;
//Loop to create the vertices and normals for a row of hexs
for (i = 0; i < numHexs; i++) {
//Calculations for hexs
r2 = radius / 2;
x1 = x - radius;
x2 = x + radius;
y1 = y - r2;
y2 = y + r2;
//Vertex assignment
vertices [(i * numHexs)] = new Vector3 (x1, 0, y1);
vertices [(i * numHexs) + 1] = new Vector3 (x1, 0, y2);
vertices [(i * numHexs) + 2] = new Vector3 ((float)(x), 0, (float)(y + radius));
vertices [(i * numHexs) + 3] = new Vector3 (x2, 0, y2);
vertices [(i * numHexs) + 4] = new Vector3 (x2, 0, y1);
vertices [(i * numHexs) + 5] = new Vector3 ((float)(x), 0, (float)(y - radius));
//Normals assignment
normals [(i * numHexs)] = Vector3.up;
normals [(i * numHexs) + 1] = Vector3.up;
normals [(i * numHexs) + 2] = Vector3.up;
normals [(i * numHexs) + 3] = Vector3.up;
normals [(i * numHexs) + 4] = Vector3.up;
normals [(i * numHexs) + 5] = Vector3.up;
//Moves the x position to the right for the next hex.
x+=radius;
}
//loop to assign triangles for each hex. I'm pretty sure this is where I'm messing up.
for (j=0; j<numHexs; j++) {
triangles [(j * numHexs)] = (j * numHexs) + 1;
triangles [(j * numHexs) + 1] = (j * numHexs) + 5;
triangles [(j * numHexs) + 2] = (j * numHexs);
triangles [(j * numHexs) + 3] = (j * numHexs) + 2;
triangles [(j * numHexs) + 4] = (j * numHexs) + 5;
triangles [(j * numHexs) + 5] = (j * numHexs) + 1;
triangles [(j * numHexs) + 6] = (j * numHexs) + 2;
triangles [(j * numHexs) + 7] = (j * numHexs) + 4;
triangles [(j * numHexs) + 8] = (j * numHexs) + 5;
triangles [(j * numHexs) + 9] = (j * numHexs) + 2;
triangles [(j * numHexs) + 10] = (j * numHexs) + 3;
triangles [(j * numHexs) + 11] = (j * numHexs) + 4;
}
Mesh mesh = new Mesh ();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
MeshFilter mesh_filter = GetComponent<MeshFilter> ();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer> ();
MeshCollider mesh_collider = GetComponent<MeshCollider> ();
mesh_filter.mesh = mesh;
}
}