我有4个对象,每个对象都位于正方形的一角。我希望按顺时针方向移动这些对象,每个方法调用一个位置。
使用我的自动取款机代码,他们只需在一次方法调用中完成整个循环,而不是只移动一个位置……
到目前为止我的代码如下:
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);
}如果任何人有任何想法如何解决这个问题,或者至少向我解释为什么它-在一个方法调用中完成整个循环……
谢谢!
发布于 2015-03-24 20:27:14
我真的不确定你的计时器是如何工作的,也不知道你的代码有什么问题。但我使用的是协程,每隔两秒,这些块就会切换,而且会持续发生。我想这应该是你需要的地方。
//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;
}希望这能有所帮助。
https://stackoverflow.com/questions/29231260
复制相似问题