在圆形编队内生成的对象的移动旋转 - Unity C#

Shift Rotation of Objects being Spawned within a Circle Formation - Unity C#

我在一个圆圈中生成了一系列 cubes/cylinders,而不是直接在之前生成的对象上做同样的事情,共 8 次。这创建了一种结构,由 Unity 元素制成。

这是目前的样子:https://imgur.com/GCdCp2c

目前,对象每次都以完全相同的方式彼此重叠,因此对象被分割成垂直直线或某种列。我想改变每个 "layer" 生成的旋转方式,这样看起来就像砖块的铺设方式一样。

我在代码中留下了评论,希望能提供某种进一步的视觉解释。

    public GameObject cylinder;
    int pieceCount = 15;
    Vector3 centerPos = new Vector3(0, -2.2f, 0);
    float radius = 1.21f;

    void Awake()
    {
        float angle = 360f / (float)pieceCount;

        for (int layers = 1; layers < 9; layers++)
        {
            for (int i = 0; i < pieceCount; i++)
            {
                Quaternion rotation = Quaternion.AngleAxis(i * angle, Vector3.up);

                Vector3 direction = rotation  * Vector3.forward;

                Vector3 position = centerPos + (direction * radius);
                Instantiate(cylinder, position, rotation);
            }
            centerPos.y += .6f;
        }
        //Currently the structure looks like this
        // [] [] [] []
        // [] [] [] []
        // [] [] [] []
        // [] [] [] []

        //Ideally something similar to this
        //[] [] [] []
        //  [] [] [] []
        //[] [] [] []
        //  [] [] [] []

    }

我以为我对这一切的数学理解得比较好,但是我在实现这个新功能时遇到了问题。我尝试了多种方法,例如将旋转变量乘以欧拉,但使用这个变量很挑剔,而且它似乎很容易影响我的对象的方向,因此它们不再相互堆叠并且结构分崩离析。

您可以在交替图层上添加 0.5f * angle 的偏移量。要获得交替,您可以将偏移量设置为 (layers % 2) * 0.5f * angle,其中 layers 从 0:

开始
void Awake()
{
    float angle = 360f / (float)pieceCount;

    for (int layers = 0; layers < 8; layers++)
    {
        float angleOffset = (layers % 2) * 0.5f * angle;
        for (int i = 0; i < pieceCount; i++)
        {
            Quaternion rotation = Quaternion.AngleAxis(i * angle + angleOffset, Vector3.up);

            Vector3 direction = rotation  * Vector3.forward;

            Vector3 position = centerPos + (direction * radius);
            Instantiate(cylinder, position, rotation);
        }
        centerPos.y += .6f;
    }
}