当我使用Quaternion按下U键时,我想要循环一个对象:
public KeyCode UTurn;
void UNotation()
{
if (Input.GetKeyDown(UTurn)) {
Quaternion rotation2 = Quaternion.Euler(new Vector3(0, 90, 0));
StartCoroutine (rotateObject(objectToRotate, rotation2, 1f));
// Debug.Log("Turn: U");
}
}
然后,我有IEnumerator
函数允许我的对象在2秒内旋转,如下所示:
bool rotating = false;
public GameObject objectToRotate;
IEnumerator rotateObject(GameObject gameObjectToMove, Quaternion newRot, float duration)
{
Debug.Log("Accessed IEnumerator function...");
if (rotating)
{
yield break;
}
rotating = true;
Quaternion currentRot = gameObjectToMove.transform.rotation;
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
Debug.Log("Rotated!");
gameObjectToMove.transform.rotation = Quaternion.Lerp(currentRot, newRot, counter / duration);
yield return null;
}
rotating = false;
}
然后我调用UNotation()
函数中的Update()
函数:
void Update () {
UNotation();
}
我希望能够一次又一次地继续旋转立方体每次旋转完成后按U键,但由于某种原因我只能让对象旋转一次< / strong>然后它不再旋转,但是Debug.Log
函数中的两个IEnumerator
调用被检查到控制台就好了,所以由于某种原因,这条线停止工作:
gameObjectToMove.transform.rotation = Quaternion.Lerp(currentRot, newRot, counter / duration);
我已尝试在功能内部进行循环,但最终导致游戏崩溃。感谢任何帮助!