Question by
themusicguy1989 · Aug 25, 2018 at 12:40 AM ·
hexagon
Trying to make a stretched Hex shaped Grid


So i'm trying to make a tactical game. I can make a square grid but, i'm trying to make a grid shaped like the bottom picture ( But with one more column on either side).
So my solution was to make the middle row first, then add three rows above if offset and delete one hex with each row. Then repeat the same thing with the rows below. But i'm stumped, this is as far as i can get without weird stuff happening. Any help would be greatly appreiciated. Thnx everyone for all the help, i'v only posted once before but i've been surfing these forums for a while. And here's my code ( i know thats its messy right now. I'll condense some things when i get it all working).
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
public class HexGrid : MonoBehaviour {
public int width = 11;
public int height = 7;
public Text cellLabelPrefab;
Canvas gridCanvas;
public HexCell cellPrefab;
HexCell[] cells;
void Awake(){
gridCanvas = GetComponentInChildren<Canvas>();
cells = new HexCell[height * width];
//original code
/*for (int z = 0, i = 0; z < height; z++)
{
for (int x = 0; x < width; x++)
{
CreateCell(x, z, i++);
}
}*/
for (int x = 0, z = 0, i = 0; x < width; x++)
{
CreateRow(x, z, i++);
}
for (int x = 0, z = 0, i = 0; z < height; z++, x++)
{
CreateColumn(x, z, i++);
}
}
void CreateRow(int x, int z, int i)
{
Vector3 position;
position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f);
position.y = 0f;
position.z = 0f;
HexCell cell = cells[i] = Instantiate<HexCell>(cellPrefab);
cell.transform.SetParent(transform, false);
cell.transform.localPosition = position;
Text label = Instantiate<Text>(cellLabelPrefab);
label.rectTransform.SetParent(gridCanvas.transform, false);
label.rectTransform.anchoredPosition =
new Vector2(position.x, position.z);
label.text = x.ToString() + "\n" + z.ToString();
}
void CreateColumn(int x, int z, int i)
{
Vector3 position;
position.x = (x + z * 0.5f - z) * (HexMetrics.innerRadius * 2f);
position.y = 0f;
position.z = z * (HexMetrics.outerRadius * 1.5f);
HexCell cell = cells[i] = Instantiate<HexCell>(cellPrefab);
cell.transform.SetParent(transform, false);
cell.transform.localPosition = position;
Text label = Instantiate<Text>(cellLabelPrefab);
label.rectTransform.SetParent(gridCanvas.transform, false);
label.rectTransform.anchoredPosition =
new Vector2(position.x, position.z);
label.text = x.ToString() + "\n" + z.ToString();
}
void CreateCell(int x, int z, int i)
{
Vector3 position;
position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f);
position.y = 0f;
position.z = z * (HexMetrics.outerRadius * 1.5f);
HexCell cell = cells[i] = Instantiate<HexCell>(cellPrefab);
cell.transform.SetParent(transform, false);
cell.transform.localPosition = position;
Text label = Instantiate<Text>(cellLabelPrefab);
label.rectTransform.SetParent(gridCanvas.transform, false);
label.rectTransform.anchoredPosition =
new Vector2(position.x, position.z);
label.text = x.ToString() + "\n" + z.ToString();
}
}
screenshot-5.png
(223.0 kB)
img-0594.jpg
(97.5 kB)
Comment
Your answer