围绕另一个gameObject旋转gameObjects

时间:2015-09-09 05:51:39

标签: unity3d rotation 2d

我在一个大圈子里有19个圈子

我需要编写这个算法

"如果点击一个集中的圆圈(周围的圆圈围绕它旋转)"

不仅旋转(围绕圆圈 - 圆圈应该等于/分配给它后面的圆圈)

检查此图片:http://i.imgur.com/HknGXFh.png

我写了一些复杂的代码,但没有用到

void OnMouseDown()
{
    if (gameObject.tag == "dynball")
    {
        if (isNotBanned(int.Parse(gameObject.name)))
        {
            change();
        }
    }
}
    void change()
{
    if (int.Parse(gameObject.name) == 4 || int.Parse(gameObject.name) == 5)
    {
        tmp = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 4];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 4] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 3];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 3] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 1];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 1] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 5];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 5] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 4];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 4] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 1];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 1] = tmp;
    }
    if (int.Parse(gameObject.name) == 8 || int.Parse(gameObject.name) == 9 || int.Parse(gameObject.name) == 10)
    {
        tmp = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 5];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 5] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 4];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 4] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 1];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 1] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 5];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 5] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 4];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 4] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 1];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 1] = tmp;
    }
    if (int.Parse(gameObject.name) == 13 || int.Parse(gameObject.name) == 14)
    {
        tmp = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 5];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 5] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 4];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 3] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 1];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 1] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 4];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 4] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 3];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) + 3] = GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 1];
        GameObject.FindGameObjectsWithTag("dynball")[int.Parse(gameObject.name) - 1] = tmp;
    }
    Debug.Log("I am here");
}

2 个答案:

答案 0 :(得分:0)

缓存你的变量man,有很多方法调用FindGameObjectsWithTag(...)int.Parse(...),我很惊讶这并不会让Unity崩溃

GameObject[] dynballs;
int gName;

void Start()
{
    gName = int.Parse(gameObject.name);
}

void OnMouseDown()
{
    if (gameObject.tag == "dynball")
    {
        if (isNotBanned(int.Parse(gameObject.name)))
        {
            change();
        }
    }
}
    void change()
{
    dynballs = GameObject.FindGameObjectsWithTag("dynball");
    if (int.Parse(gameObject.name) == 4 || int.Parse(gameObject.name) == 5)
    {
        tmp = dynballs[gName - 4];
        dynballs[gName - 4] = dynballs[gName - 3];
        dynballs[gName - 3] = dynballs[gName + 1];
        dynballs[gName + 1] = dynballs[gName + 5];
        dynballs[gName + 5] = dynballs[gName + 4];
        dynballs[gName + 4] = dynballs[gName - 1];
        dynballs[gName - 1] = tmp;
    }
    if (int.Parse(gameObject.name) == 8 || int.Parse(gameObject.name) == 9 || int.Parse(gameObject.name) == 10)
    {
        tmp = dynballs[int.Parse(gameObject.name) - 5];
        dynballs[gName - 5] = dynballs[gName - 4];
        dynballs[gName - 4] = dynballs[gName + 1];
        dynballs[gName + 1] = dynballs[gName + 5];
        dynballs[gName + 5] = dynballs[gName + 4];
        dynballs[gName + 4] = dynballs[gName - 1];
        dynballs[gName - 1] = tmp;
    }
    if (int.Parse(gameObject.name) == 13 || int.Parse(gameObject.name) == 14)
    {
        tmp = dynballs[gName - 5];
        dynballs[gName - 5] = dynballs[gName - 4];
        dynballs[gName - 3] = dynballs[gName + 1];
        dynballs[gName + 1] = dynballs[gName + 4];
        dynballs[gName + 4] = dynballs[gName + 3];
        dynballs[gName + 3] = dynballs[gName - 1];
        dynballs[gName - 1] = tmp;
    }
    Debug.Log("I am here");
}

但无论如何,iv&#e回答了类似的问题herehere,您可能会发现答案很有用

答案 1 :(得分:-1)

使用transform.rotateAround或quaternion.slerp