【发布时间】:2021-03-28 02:49:48
【问题描述】:
所以我试图让我的 update() 函数在玩家每次按下按钮“C”时等待几秒钟,因为每次当玩家按下键“c”时它都会重置对象的旋转并且我正在尝试让我的游戏等待几秒钟,因为它会使对象的一些小动画重置他的旋转值。
void Reset()
{
Vector3 newRotation =
gameObject.transform.rotation.eulerAngles;
if (Input.GetKeyDown(KeyCode.C))
{
x = newRotation.x;
y = newRotation.y;
z = newRotation.z;
x = Mathf.Round(x);
y = Mathf.Round(y);
z = Mathf.Round(z);
yield return new WaitForSeconds(1);
print(x + " " + y + " " + z);
for (; x >= 0; x--)
{
arotation.x = x;
boxy.transform.Rotate(arotation.x, y, z);
if (x == 0)
{
for (; y >= 0; y--)
{
arotation.y = y;
boxy.transform.Rotate(arotation.x, arotation.y, z);
if (y == 0)
{
for (; z >= 0; z--)
{
arotation.z = z;
boxy.transform.Rotate(arotation.x, arotation.y, arotation.z);
}
}
}
}
}
print(x + " " + y + " " + z);
}
}
【问题讨论】:
-
您不能阻止
Update方法。这将冻结你的游戏。你可以尝试使用协程。 -
你需要使用协程,所以你会得到
IEnumerator Reset()而不是返回void Reset(),那样yield return new WaitForSeconds(1)实际上会等待一秒钟。请注意,调用函数将使用StartCoroutine(Reset());而不仅仅是Reset();docs.unity3d.com/Manual/Coroutines.html