【发布时间】:2023-03-18 06:08:02
【问题描述】:
我有 4 个对象,每个对象都位于正方形的一个角落。我希望顺时针移动这些对象,每个方法调用 1 个位置。
使用我的 atm 代码,它们都只是在 1 个方法调用上完成整个循环,而不是只移动一个位置...
到目前为止我的代码:
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);
}
如果有人对如何解决这个问题有任何想法,或者至少向我解释一下为什么它会在 1 个方法调用中完成整个循环......
谢谢!
【问题讨论】:
-
这叫什么? (这段代码调用了多少次)
-
有一个定时器,2秒。方法完成后它会重置。计时器倒计时使用 -= Time.deltaTime @Sayse
-
那么它被调用了多少次?代码本身看起来不像问题(对我来说),看起来更像是它被多次调用,要么所有 4 个对象都在执行此代码,要么该计时器执行了太多次
-
@Sayse 代码应该永远循环,至少这就是整个想法。我想每 2 秒更改一次对象的位置。他们应该永远改变立场。
-
很好,应该可以,我想你需要看看计时器,我对统一不太熟悉,但我相信它有自己的游戏循环,你应该看看与之交互而不是创建一个单独的计时器。 (跟踪自调用此方法以来的最后一次时间,仅在大于 2 秒时调用)