Instantiate GameObjects in a Sphere shape

Hi all,

I’m trying to figure out a way to Instantiate a bunch of GameObjects (‘flat’ hexagons) to create a sphere. Essentially a spherical TileMap.

Is this actually possible and would anyone be able to point me in the right direction for a solution?


If you know the radius of the hexagon you can calculate everything needed:

It’s important to remember that in a regular hexagon the length of each side is always equal to the radius. We need to envision the hexagon oriented this way:
Where the blue line is the radius:

The height is then equal to radius*2

The width is then equal to radius*sqrt(3)

Every odd numbered layer will be horizontally offset by width/2

Every even numbered layer will not be horizntally offset.

Every layer will have a vertical offset of height*0.75

Now we’ll go through a 2D loop:

float ySpacing = height * 0.75f;
float xStagger = width / 2f;

int xCount = Mathf.CeilToInt(circleRadius / width);
int yCount = Mathf.CeilToInt(circleRadius / ySpacing);

float circleRadiusSqr = circleRadius * circleRadius;

// In cases where x is staggered by y position additional x unit is needed
// Thus the -1
for (int x = -xCount - 1; x <= xCount; x++) {
    for (int y = -yCount; y <= yCount; y++) {
        float yOffset = y * ySpacing;
        float xOffset = x * width + ((y % 2) * xStagger);

        if (xOffset * xOffset + yOffset * yOffset <= circleRadiusSqr)
            Vector3 position = origin + xAxis * xOffset;
            position = position + yAxis * yOffset;

            // Do instantiation using position vector, replace xAxis and yAxis with any vector
            // Pointing in that axis, standard axis use the axis defined
            // in Vector3 (such as Vector3.up) or your own. If creating a 2D scene, use Vector2

If you need a sphere, not just a circle repeat this multiple times with a different origin on the axis you don’t modify in the loop above. The new radius at any given hgieht in a sphere can be obtained with: sqrt(circleRadiusSqr - sampleHeight * sampleHeight) Where sampleHeight is the height of a sphere where you wish to get the horizontal radius, equal to getting the width of a circle at a specific height.

Hope this was clear enough to help.

I’ve not had a chance to try this out yet, but I think I understand the thinking. Excellent explanation, thank you, really appreciate it.

So, to see if I have it right in my head…

Define width/height (based on hex width)
Define circle radius (for a ring of hexes)

For a circle use the equation in the last paragraph with a ‘new’ sampleHeight (currentHeight + radius*2) - simple example and repeat for amount of ‘rows’ needed to create sphere.

Assuming we start at sampleHeight=0, 2 loops, 1 for +y sampleHeight, 1 for -y sampleHeight ?