Hey there, new to unity and C#. I have managed to create a rectangular hex grid but I need to create a hexagonal map of hexagons. Creating a rectangular grid starts in the bottom right corner, where a hexagonal shaped map starts at 0,0,0 which is what I am aiming for. Using Redblobgames. Looking at the pseudo code for hexagonal shape:
unordered_set<Hex> map;
for (int q = -map_radius; q <= map_radius; q++) {
int r1 = max(-map_radius, -q - map_radius);
int r2 = min(map_radius, -q + map_radius);
for (int r = r1; r <= r2; r++) {
map.insert(Hex(q, r, -q-r));
}
}
I’m having a hard time understanding what to do with
int r1 = max(-map_radius, -q - map_radius);
int r2 = min(map_radius, -q + map_radius);
It doesn’t appear to be a method, but more like a Vector2, but I had troubles implementing that.
my current code:
public class CubeHexGrid : MonoBehaviour {
public GameObject HexPrefab;
public int x = 5;
public int y = 5;
private float offsetX, offsetY;
// Use this for initialization
void Start()
{
Vector3 bounds = HexPrefab.GetComponent<MeshRenderer>().bounds.extents;
float hexWidth = bounds.x * 2;
float horzOffset = hexWidth * (3 / 4);
float sqrt = Mathf.Sqrt(3) / 2;
float hexHeight = sqrt * hexWidth;
float vertOffset = hexHeight;
offsetX = hexHeight * Mathf.Sqrt(3);
offsetY = hexHeight * .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(HexPrefab, 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;
}
}
Any thoughts on that first bit of pseudocode to help figure out what it means?