Hex grid in C Sharp

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;
        }
}
for (i = 0; i < numHexs; i++) {

    vertices [(i * numHexs)] = ...;
    vertices [(i * numHexs) + 1] = ...;
    vertices [(i * numHexs) + 2] = ...;
    vertices [(i * numHexs) + 3] = ...;
    vertices [(i * numHexs) + 4] = ...;
    vertices [(i * numHexs) + 5] = ...;

    ...

numHexs = 2
i= 0 : 0 1 2 3 4 5 6
i = 1: 2 3 4 5 6 7 8
i = 2: 3 4 5 6 7 8 9

I’m fairly sure you’re looking for hex0 to be 0-5, hex1 to be 6-11?

that would be

vertices[(i*6)] ...
vertices[(i*6)+1] ...
vertices[(i*6)+2] ...

Yes I want the first hex to be vertices 0-5, the second to be vertices 6-11 and so on. I changed my code to match your changes but am still getting a strange outcome. I can’t post a picture of what is showing in Unity but I will as soon as I can.

Thanks for your help.

I grabbed this from another forum users’ signature a little while back, been meaning to look over it but I’ve not had the time might be worth a look: [OPEN SOURCE]Procedural Hexagon Terrain - Learn Content & Certification - Unity Discussions

That’d be me! :smile:

Yes I have read through that post several times. The first part helped me greatly in understanding how to actually create the mesh with the correct coordinates. In my case the mesh I am trying to create is going to be the same every time. (Specifically 91 hexs arranged in a larger hex pattern with 6 hexs per side)

I am trying to recreate a board game in a digital fashion, however I cannot seem to get this loop to function correctly. Do you think it would be easier to make a hex prefab and then use a loop to instantiate 91 of them or maybe draw out one large mesh with preset vertices?

Thanks in advance

Not exactly sure what you mean by:

Are you asking if you should do it like in my post, with chunking? Using separate gameObjects is vastly more easy, at the cost of performance. However, with only 91 hexs, I don’t think you’d get much out of chunking. Prefabs looks fine and easy to me!

Yes I meant like the chunking you demonstrated in your post. I guess prefabs would be much easier. Thanks!

Yes, I would just spawn some prefabs. The same concepts applies for positioning. Glad to see an increase in hexagon grids lately.