Search code examples
c#for-loopunity-game-enginehexagonal-tiles

Nested For Loops - generating a catan-style hex-grid in Unity


As the title suggests, I am trying to generate a procedural hex grid in Unity using C#.

void Start () 
{
    //5 mid
     startRowSize = 5;

    for (int i = 0; i < startRowSize; i++)
    {
        GameObject newHex = (GameObject)Instantiate(hex);
        hexWidth = newHex.gameObject.renderer.bounds.size.z;
        newHex.transform.Rotate(new Vector3(0,30,0));
        newHex.transform.position = new Vector3((i * hexWidth),0,0);
    }

    for (int row = 0; row <= startRowSize-1; row ++)
    {
    for (int i = 0; i < row; i++)
    {
        GameObject newHex = (GameObject)Instantiate(hex);
        newHex.transform.Rotate(new Vector3(0,30,0));
        newHex.transform.position = new Vector3(((i*hexWidth)+((hexWidth/2))+(row*(hexWidth/2))),0,((startRowSize-row))*-(hexWidth/1.17f));
    }
    }
}

The code works, however the rows are being generated "backwards", meaning the outer rows contain the higher amounts of hexes, while the inner rows contain the smallest.

This is obviously the opposite effect that I am trying to achieve. I've been messing with this code for hours and I can't figure out why.

Any thoughts?


Solution

  • So after a few more hours of messing around with the code, I figured out why it wasn't iterating properly. He's the refined code...

    void Start () 
    {
        //5 mid
         startRowSize = 10;
    
        for (int i = 0; i < startRowSize; i++)
        {
            GameObject newHex = (GameObject)Instantiate(hex);
            hexWidth = newHex.gameObject.renderer.bounds.size.z;
            newHex.transform.Rotate(new Vector3(0,30,0));
            newHex.transform.position = new Vector3((i * hexWidth),0,0);
        }
    
        for (int row = 0; row < startRowSize; row++)
        {
            for (int i = 0; i < startRowSize-1-row; i++)
                {
                    GameObject newHex = (GameObject)Instantiate(hex);
                    newHex.transform.Rotate(new Vector3(0,30,0));
    
                if (row == 0)
                    {
                        newHex.transform.position = new Vector3(((i*hexWidth)+(hexWidth/2)),0,-(hexWidth/1.17f));
                    }
                else
                    {
                        newHex.transform.position = new Vector3((i*hexWidth)+((row+1)*(hexWidth/2)),0,(row+1)*-(hexWidth/1.17f));
                    }
                }
        }
    
        for (int row = 0; row < startRowSize; row++)
        {
            for (int i = 0; i < startRowSize-1-row; i++)
                {
                    GameObject newHex = (GameObject)Instantiate(hex);
                    newHex.transform.Rotate(new Vector3(0,30,0));
    
                if (row == 0)
                    {
                        newHex.transform.position = new Vector3(((i*hexWidth)+(hexWidth/2)),0,(hexWidth/1.17f));
                    }
                else
                    {
                        newHex.transform.position = new Vector3((i*hexWidth)+((row+1)*(hexWidth/2)),0,(row+1)*(hexWidth/1.17f));
                    }
                }
        }
    }
    

    Now, can anyone suggest how to clean it up a bit? My brain is fizzled...