Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Hex grid in C Sharp

Discussion in 'Scripting' started by tamhelsel, Jul 14, 2014.

  1. tamhelsel

    tamhelsel

    Joined:
    Jul 14, 2014
    Posts:
    4
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(MeshFilter))]
    5. [RequireComponent(typeof(MeshRenderer))]
    6. [RequireComponent(typeof(MeshCollider))]
    7.  
    8.  
    9. public class MeshCreator : MonoBehaviour
    10. {
    11.  
    12.         public float radiusval;
    13.         public float xval;
    14.         public float yval;
    15.         public int numHexs;
    16.  
    17.         // Use this for initialization
    18.         void Start ()
    19.         {
    20.                 createHex (xval, yval, radiusval);
    21.         }
    22.    
    23.         // Update is called once per frame
    24.         void Update ()
    25.         {
    26.    
    27.         }
    28.  
    29.         void createHex (float _x, float _y, float _radius)
    30.         {
    31.                 //Variables for calculations
    32.                 float r2, x1, x2, y1, y2;
    33.                 float x = _x;
    34.                 float y = _y;
    35.                 float radius = _radius;
    36.            
    37.                 //Arrays for vertices, normals, and traingles. Using the *2 to allow for large grids of hexs
    38.                 Vector3[] vertices = new Vector3 [(6 * numHexs) * 2];
    39.                 Vector3[] normals = new Vector3[(6 * numHexs) * 2];
    40.  
    41.                 int [] triangles = new int [(12 * numHexs) * 2];
    42.  
    43.                 int i;
    44.                 int j;
    45.                
    46.                 //Loop to create the vertices and normals for a row of hexs
    47.                 for (i = 0; i < numHexs; i++) {
    48.  
    49.                         //Calculations for hexs
    50.                         r2 = radius / 2;
    51.                         x1 = x - radius;
    52.                         x2 = x + radius;
    53.                         y1 = y - r2;
    54.                         y2 = y + r2;
    55.                    
    56.                         //Vertex assignment
    57.                         vertices [(i * numHexs)] = new Vector3 (x1, 0, y1);
    58.                         vertices [(i * numHexs) + 1] = new Vector3 (x1, 0, y2);
    59.                         vertices [(i * numHexs) + 2] = new Vector3 ((float)(x), 0, (float)(y + radius));
    60.                         vertices [(i * numHexs) + 3] = new Vector3 (x2, 0, y2);
    61.                         vertices [(i * numHexs) + 4] = new Vector3 (x2, 0, y1);
    62.                         vertices [(i * numHexs) + 5] = new Vector3 ((float)(x), 0, (float)(y - radius));
    63.  
    64.                         //Normals assignment
    65.                         normals [(i * numHexs)] = Vector3.up;
    66.                         normals [(i * numHexs) + 1] = Vector3.up;
    67.                         normals [(i * numHexs) + 2] = Vector3.up;
    68.                         normals [(i * numHexs) + 3] = Vector3.up;
    69.                         normals [(i * numHexs) + 4] = Vector3.up;
    70.                         normals [(i * numHexs) + 5] = Vector3.up;
    71.                        
    72.                         //Moves the x position to the right for the next hex.
    73.                         x+=radius;
    74.  
    75.                 }
    76.  
    77.                 //loop to assign triangles for each hex. I'm pretty sure this is where I'm messing up.
    78.                 for (j=0; j<numHexs; j++) {
    79.                         triangles [(j * numHexs)] = (j * numHexs) + 1;
    80.                         triangles [(j * numHexs) + 1] = (j * numHexs) + 5;
    81.                         triangles [(j * numHexs) + 2] = (j * numHexs);
    82.            
    83.                         triangles [(j * numHexs) + 3] = (j * numHexs) + 2;
    84.                         triangles [(j * numHexs) + 4] = (j * numHexs) + 5;
    85.                         triangles [(j * numHexs) + 5] = (j * numHexs) + 1;
    86.            
    87.                         triangles [(j * numHexs) + 6] = (j * numHexs) + 2;
    88.                         triangles [(j * numHexs) + 7] = (j * numHexs) + 4;
    89.                         triangles [(j * numHexs) + 8] = (j * numHexs) + 5;
    90.            
    91.                         triangles [(j * numHexs) + 9] = (j * numHexs) + 2;
    92.                         triangles [(j * numHexs) + 10] = (j * numHexs) + 3;
    93.                         triangles [(j * numHexs) + 11] = (j * numHexs) + 4;
    94.                 }
    95.  
    96.                 Mesh mesh = new Mesh ();
    97.                 mesh.vertices = vertices;
    98.                 mesh.triangles = triangles;
    99.                 mesh.normals = normals;
    100.                
    101.                 MeshFilter mesh_filter = GetComponent<MeshFilter> ();
    102.                 MeshRenderer mesh_renderer = GetComponent<MeshRenderer> ();
    103.                 MeshCollider mesh_collider = GetComponent<MeshCollider> ();
    104.  
    105.                 mesh_filter.mesh = mesh;
    106.         }
    107. }
    108.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. for (i = 0; i < numHexs; i++) {
    3.  
    4.     vertices [(i * numHexs)] = ...;
    5.     vertices [(i * numHexs) + 1] = ...;
    6.     vertices [(i * numHexs) + 2] = ...;
    7.     vertices [(i * numHexs) + 3] = ...;
    8.     vertices [(i * numHexs) + 4] = ...;
    9.     vertices [(i * numHexs) + 5] = ...;
    10.  
    11.     ...
    12.  
    13.  
    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

    Code (csharp):
    1.  
    2. vertices[(i*6)] ...
    3. vertices[(i*6)+1] ...
    4. vertices[(i*6)+2] ...
    5.  
     
  3. tamhelsel

    tamhelsel

    Joined:
    Jul 14, 2014
    Posts:
    4
    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.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  5. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
  6. tamhelsel

    tamhelsel

    Joined:
    Jul 14, 2014
    Posts:
    4
    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
     
  7. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    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!
     
  8. tamhelsel

    tamhelsel

    Joined:
    Jul 14, 2014
    Posts:
    4
    Yes I meant like the chunking you demonstrated in your post. I guess prefabs would be much easier. Thanks!
     
  9. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Yes, I would just spawn some prefabs. The same concepts applies for positioning. Glad to see an increase in hexagon grids lately.