I am trying to instantiate some hexagons around a central hexagon but I'm getting an odd gap between some of them and I cant figure out what's causing it. The hexagons on the top and bottom should be closer to the white hexagon in the middle. I've read up on this site CatLikeCodiing on how to create hexagonal grids and the math looks right. Any ideas what the issues may be?
outerRadius = hexButton.GetComponent<RectTransform>().rect.x;
innerRadius = (Mathf.Sqrt(3) / 2) * outerRadius;
Debug.Log(string.Format("OUTER RADIUS: {0}", outerRadius));
Debug.Log(string.Format("INNER RADIUS: {0}", innerRadius));
startPos = Instantiate(hexButton, Vector3.zero, hexButton.transform.rotation);
startPos.transform.SetParent(GameObject.Find("PanelMain").transform, false);
startPos.name = string.Format("Start Hex");
for (int i = 0; i < level; i++)
{
if (i <= 6)
{
var temp = Instantiate(hexButton, new Vector3(outerRadius * 2, 0, 0), hexButton.transform.rotation);
temp.transform.RotateAround(hexButton.transform.position, Vector3.back, 60 * i);
temp.transform.rotation = hexButton.transform.rotation;
temp.transform.SetParent(GameObject.Find("PanelMain").transform, false);
temp.GetComponent<Button>().image.color = Color.green;
temp.name = string.Format("Hex: {0}", i);
}
}
FIX
#region
outerRadius = hexButton.GetComponent<RectTransform>().rect.y * 0.5f;
innerRadius = (Mathf.Sqrt(3) / 2) * outerRadius;
hexWidth = hexButton.GetComponent<RectTransform>().rect.width;
offset = hexWidth * 0.93333333333f;
Debug.Log(string.Format("OUTER RADIUS: {0}", outerRadius));
Debug.Log(string.Format("INNER RADIUS: {0}", innerRadius));
Debug.Log(string.Format("WIDTH: {0}", gap));
startPos = Instantiate(hexButton, Vector3.zero, hexButton.transform.rotation);
startPos.transform.SetParent(GameObject.Find("PanelMain").transform, false);
startPos.name = string.Format("Start Hex");
for (int i = 0; i < level -1 ; i++)
{
if (i <= 6)
{
var temp = Instantiate(hexButton, new Vector3(offset, 0, 0), hexButton.transform.rotation);
temp.transform.RotateAround(hexButton.transform.position, Vector3.back, 60 * i);
temp.transform.rotation = hexButton.transform.rotation;
temp.transform.SetParent(GameObject.Find("PanelMain").transform, false);
temp.GetComponent<Button>().image.color = Color.green;
temp.name = string.Format("Hex: {0}", i);
}
}
#endregion