Generate Hexgrid
[9157-screenshot.20.png*_|9157]
I am going to walk you through on how to do the calculation. The line on the left is 1 unit long. I am going to skip the trigonometry part and go straight to the calculation
The center of the blue hex is ( 0, 0 ), the center of the green is ( root(3) * unit-radius, 0 ), and the center of the red hex is ( root(3) * unit-radius , 1.5 * unit-radius).
By controlling the unit length, you can scale up and scale down the size of the grid. In this example, the radius of the circle is root(3), if you want to have a circle with radius of 1 unit; you can simply divide the calculations by (root(3)/2) to scale up the grid such that they are built from a circle with 1 unit radius.
using UnityEngine;
using System.Collections;
public class HexGrid : MonoBehaviour {
public Transform spawnThis;
public int x = 5;
public int y = 5;
public float radius = 0.5f;
public bool useAsInnerCircleRadius = true;
private float offsetX, offsetY;
void Start() {
float unitLength = ( useAsInnerCircleRadius )? (radius / (Mathf.Sqrt(3)/2)) : radius;
offsetX = unitLength * Mathf.Sqrt(3);
offsetY = unitLength * 1.5f;
for( int i = 0; i < x; i++ ) {
for( int j = 0; j < y; j++ ) {
Vector2 hexpos = HexOffset( i, j );
Vector3 pos = new Vector3( hexpos.x, hexpos.y, 0 );
Instantiate(spawnThis, pos, Quaternion.identity );
}
}
}
Vector2 HexOffset( int x, int y ) {
Vector2 position = Vector2.zero;
if( y % 2 == 0 ) {
position.x = x * offsetX;
position.y = y * offsetY;
}
else {
position.x = ( x + 0.5f ) * offsetX;
position.y = y * offsetY;
}
return position;
}
}
Attach this to a empty, assign a default-sphere.
Random/Controlled grid-generation
Random Generation
Make these changes to the script.
public Transform [] spawnThis;
....
Instantiate( spawnThis[ Random.Range(0,spawnThis.length) ], ..., ... );
Controlled Generation
public Transform [] tile; // Water, Ground, Tree tiles
public int W, G, T; //Assign the index of the tiles in the tile[] to here;
private int [][] formation =
{
{ W, W, W, W, W, W, W },
{ W, G, G, G, G, G, W },
{ W, G, T, T, T, G, W },
{ W, G, G, G, G, G, W },
{ W, W, W, W, W, W, W },
}
....
for( int i = 0; i < formation.length; i++ ) {
for( int j = 0; j < formation*.length; j++ ) {*
Vector2 hexpos = HexOffset( i, j );
Vector3 pos = new Vector3( hexpos.x, hexpos.y, 0 );
Instantiate(tile[ formation*[j] ], pos, Quaternion.identity );*
}
}
For controlled generation, this is not the best way to do it, the formation will grew very large when you need a large map. To get a better controlled generation of grid, you will need to find out a way to generate the formation instead of defining it in your script.
_*
but all sphere are generated behind of my plane. so what do i need to do?
– Ekta-Mehta-DOn you screenshot there are hexagons, while the code uses the circle. Did you use a sprite for an hexagon or there is a way to create one programmatic?
– obiolgBut if I want to not make a completely full pile but i want some groove between the objects in the grid?
– anon79217608