首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一体化中的运动循环

一体化中的运动循环
EN

Stack Overflow用户
提问于 2015-03-24 19:20:59
回答 1查看 867关注 0票数 1

我有4个对象,每个对象都位于正方形的一角。我希望按顺时针方向移动这些对象,每个方法调用一个位置。

使用我的自动取款机代码,他们只需在一次方法调用中完成整个循环,而不是只移动一个位置……

到目前为止我的代码如下:

代码语言:javascript
复制
void SwitchPositions() 
    {

            tempPosition1 = parent.transform.GetChild(0).GetComponent<Transform>().position;
            tempPosition2 = parent.transform.GetChild(1).GetComponent<Transform>().position;
            tempPosition3 = parent.transform.GetChild(2).GetComponent<Transform>().position;
            tempPosition4 = parent.transform.GetChild(3).GetComponent<Transform>().position;
            parent.transform.GetChild (0).GetComponent<Transform> ().position = tempPosition2;
            parent.transform.GetChild (1).GetComponent<Transform> ().position = tempPosition3;
            parent.transform.GetChild (2).GetComponent<Transform> ().position = tempPosition4;
            parent.transform.GetChild (3).GetComponent<Transform> ().position = tempPosition1;
            Debug.Log (tempPosition1);


    }

如果任何人有任何想法如何解决这个问题,或者至少向我解释为什么它-在一个方法调用中完成整个循环……

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-03-24 20:27:14

我真的不确定你的计时器是如何工作的,也不知道你的代码有什么问题。但我使用的是协程,每隔两秒,这些块就会切换,而且会持续发生。我想这应该是你需要的地方。

代码语言:javascript
复制
//Predefined positions where objects to place
public Transform[] Position;
//The objects that will will be swapped in coroutines
public Transform[] ObjectsToMove;
private int ObjectIndex = 0;
private bool startupdate = true;

void Update () {
    if(startupdate)
       StartCoroutine(SwitchBlocks());
}

IEnumerator SwitchBlocks() {
    startupdate = false;
    int tempIndex = ObjectIndex;
    for(int i = 0; i < ObjectsToMove.Length; i++) {
        tempIndex = ObjectIndex + i;
        if(tempIndex > ObjectsToMove.Length - 1)
            tempIndex -= ObjectsToMove.Length;
        ObjectsToMove[i].position = Position[tempIndex].position;
    }
    ObjectIndex++;
    if(ObjectIndex > ObjectsToMove.Length - 1) {
        ObjectIndex = 0;
    }
    yield return new WaitForSeconds(2.0f);
    startupdate = true;
    yield return null;
}

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29231260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档