[英]How to use a coroutine inside the Update in Unity to wait for seconds to rotate the camera? (C#)
這是我的代碼的一部分:
void Update(){
CameraFlip();
}
void CameraFlip(){
Quaternion target = Quaternion.Euler (0, 0, 180);
cameraTransform.rotation = Quaternion.Slerp (cameraTransform.rotation, target, Time.deltaTime * 4.0f);
Debug.Log (cameraTransform.rotation.ToString ());
StartCoroutine("CameraFlipCoroutine");
}
public IEnumerator CameraFlipCoroutine(){
yield return new WaitForSeconds (Random.Range(6f,12f));
if (gameIsPlaying == false)
yield return null;
Quaternion target = Quaternion.Euler (0, 0, 0);
cameraTransform.rotation = Quaternion.Slerp (cameraTransform.rotation, target, Time.deltaTime * 4.0f);
Debug.Log (cameraTransform.rotation.ToString ());
}
因此,如果更新方法發生問題,我想做的就是將相機旋轉180度。 (例如,玩家擊中了一個敵人),幾秒鍾后,攝像頭又回到了原始位置。
在我的代碼中,第一部分是可以的,它可以按我的意願旋轉,但是當協程部分到來時,它開始旋轉,但是滯后並且不平滑。
我不知道。 還有其他方法嗎?
我想這是滯后的,因為您多次調用它。在Update()上,每秒每秒多次調用CameraFlip()函數。
您可以執行以下操作來查看是否存在問題:創建一個bool變量,如下所示:
private bool testingFlip = false;
然后在需要時檢查它,例如:
void Update(){
if (!testingFlip){`
CameraFlip();
testingFlip = true;
}
}
在某些時候,您再次使它變得虛假,以便在需要時使用它。 這不是幻想,但它是測試這是否是導致延遲的好方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.