這是我的代碼..我已經解釋了這個問題..
public bool isPipeActive;
[SerializeField] private float timer;
void Start()
{
isPipeActive = false;
timer = 0f;
}
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Backspace))
{
isPipeActive = !isPipeActive; //bool flag invert
}
if (isPipeActive == true) //start pipe animations
{
for (int j = 0; j < ParentOf_arrow.transform.childCount; j )
{
Initiate_arrow_FLOW_INDICATOR(j);
StartCoroutine(Time_Delay());
//while (timer < 1.0f)
for (timer = 0f; timer < 2.0f; timer = Time.fixedDeltaTime)
{
//just a blank function to see if it creates a delay..
timer = Time.fixedDeltaTime;
}
//timer = 0f;
}
}
}
IEnumerator Time_Delay()
{
yield return new WaitForSeconds(2f);
Debug.Log("2s delay initiated");
}
Initiate_arrow_FLOW_INDICATOR(j) 明顯地改變一系列 3D 箭頭(箭頭 1,然后是箭頭 2 等)中一個 3D 箭頭的顏色以顯示方向。像這樣的東西..我只想在每次顏色變化之間添加一個時間延遲,但是當我進入播放模式并按退格鍵時,我所做的嘗試似乎都沒有造成時間延遲。
顏色閃爍得非常快。在顏色變化之間沒有任何等待.. 即使同時運行協程、while 回圈和 for 回圈:(
發生了什么事,我該如何解決?
uj5u.com熱心網友回復:
for 回圈的問題是您不是在等待固定更新之間的實際時間,而是使用上次固定更新之間的時間并以 CPU 的速度添加它,直到計時器大于 2。協程將等待記錄它的輸出前 2 秒。您需要將箭頭更改代碼放在協程中,因為協程本質上與您的 FixedUpdate“并行”作業。
所以要么這樣做:
void FixedUpdate()
{
timer = Time.deltaTime;
if(timer >= 2.0f) {
//arrow thingy
timer = 0;
}
}
或在協程中:
IEnumerator TimeDelay()
{
yield return new WaitForSeconds(2f);
//arrow thingy
}
在這種狀態下,FixedUpdate 版本將無限回圈并大約每兩秒呼叫一次箭頭,協程將這樣做一次。
一個快速的旁注,您可能應該為此使用 Update 和 DeltaTime,而不是 FixedUpdate 和 FixedDeltaTime。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461616.html
下一篇:統一相機圍繞物件旋轉以平滑停止
