【问题标题】:Instantiating hexagons in a circle around an object在对象周围实例化六边形
【发布时间】:2021-04-05 04:14:58
【问题描述】:

我正在尝试在中心六边形周围实例化一些六边形,但其中一些之间出现了奇怪的差距,我无法弄清楚是什么原因造成的。顶部和底部的六边形应该更靠近中间的白色六边形。我已经在这个网站CatLikeCodiing 上阅读了有关如何创建六边形网格的信息,并且数学看起来是正确的。任何想法可能是什么问题?

Hex Grid Image

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);
     }            

}

修复

Hex Fixed

#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

【问题讨论】:

  • 我相信您的问题是使用 .rect.x。您链接的教程指定外半径是从中心到六边形任意角的距离。如果您查看示例中显示的视觉效果,如果您要在此处获取图像的宽度,则不会是这个长度。一个快速而肮脏的解决方法是选中预制件 Preseve Aspect Ratio 的 Image 组件上的框为 true,然后从 outerRadius 中减去值,直到它看起来正确。
  • 嗨 user3657449 感谢您指出我更新了代码以使用 rect.y / 0.5f 来获得正确的外半径。

标签: c# unity3d math


【解决方案1】:

如果您的六边形是对称的,则代码将按预期工作,这意味着rect.x == rect.y。但是您的六边形不对称,您的rect.yrect.x 略少。
代码将六边形放在围绕中心六边形的理想圆上,如果您想象一个半径为rect.x * 2 且中心位于中心点的圆,您会发现它们肯定放置正确。 如何解决你在这里得到的东西?好吧,我宁愿从头开始编写一些新代码,以便考虑六边形的比例。例如,如果您不调整与中心点的角度和/或距离,则使用 transform.Rotate() 每个旋转 60 度的简单旋转方法将无法可靠地工作。它已经有一些偏移,只有一层六边形几乎看不到,但是如果你在绿色六边形周围添加另外 12 个六边形,而不是另外 24 个,左右两个六边形之间的间隙会越来越大。在图像上,您已经可以看到左上角和右上角六边形之间有一点间隙(左下角和右下角相同),但不应该有间隙,就像左边没有间隙一样,中心和右六边形。是的,也是没有考虑比例造成的。
基本上,要正确放置这样的不对称六边形,您必须将它们沿椭圆而不是圆形放置。
很抱歉没有提供代码示例。也许你真正需要的只是让六边形对称,它已经很好了,所以尝试找到一个新的公式是没有意义的。
但是,这里有一个关于在给定角度获得椭圆点的问题:How to find the point on ellipse given the angle
您可以使用它在 60 度的椭圆上找到一个点以将右上角的十六进制放置在该位置,而不是在 120 度的椭圆上找到一个点以放置左上角的十六进制,依此类推。

【讨论】:

  • 嗨 Emriq,感谢您如此清楚地解释实际发生的情况。我重新制作了六边形图像并确保它是对称的。我还更新了 user3657449 给出的关于使用 rect.y * 0.5f 获得正确外半径的建议的代码,现在看起来好多了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
相关资源
最近更新 更多