Question

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?

Was it helpful?

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...

OTHER TIPS

Just a tip, Awake happens before Start().

void Awake() 
{
   //5 mid
  startRowSize = 10;

  for (int i = 0; i < startRowSize; i++)
  {
      GameObject newHex = HexCreator();
      hexWidth = newHex.gameObject.renderer.bounds.size.z;
      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 = HexCreator();

         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 = HexCreator();

        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));
     }
}

Since you have this code repeating I made it into a function. That way if you need to upgrade it all you do is go to the function and change it there.

private GameObject HexCreator()
{
    GameObject newHex = (GameObject)Instantiate(hex);
    newHex.transform.Rotate(new Vector3(0,30,0));

    return newHex;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top