我有一個Coroutine
,我嘗試過使用WaitForSecondsRealtime
和WaitForSeconds
。當我運行Coroutine
時,延遲在幾次迭代中是相同的,但是延遲時間稍長一些,然後延遲,然後再次出現延遲。協程正在導致對象之間的間距不均
下面是正在發生的事情的圖像,因爲您可以看到圓圈之間有一些間隙,我希望間隔更加穩定。
這裏是Coroutine
我使用這個:
public float shootDelay = 0.03f;
IEnumerator Shoot(Vector3 direction) {
direction.z = 0f;
foreach (GameObject ball in balls) {
float speed = ball.GetComponent<Ball>().speed;
ball.GetComponent<Ball>().rb.velocity = direction.normalized * speed;
yield return new WaitForSecondsRealtime(shootDelay);
}
}
這傷害了我的眼睛。 *我希望圓圈之間的差距完全相同! – djv
除非您修改'Time.timeScale'變量,否則不需要使用'WaitForSecondsRealtime()'。你目睹的錯誤是由於Unity沒有完全等待'shootDelay',而是在完成shootDelay之後的_frame。 –
我很好奇。如果您使用0.02f作爲拍攝延遲,會發生什麼情況?同樣的結果? 0.3f或0.5f怎麼樣? – JuanR