2017-10-15 179 views
2

我在下面的代碼中只使用Quad創建滾動背景。我的問題是如何在一段時間後停止滾動背景。例如,我希望在我的滾動圖像結束後,鎖定最後一個可見部分作爲關卡其餘部分的背景。由於我的播放器速度不變,因此我想象了這樣的事情:大概20秒後,停止滾動並保持圖像成爲可能。我對Unity非常陌生,我不確定如何去做,也沒有找到一種可行的方法。我將不勝感激幫助!如何在特定時間後停止紋理滾動

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class BG : MonoBehaviour 
{ 

    public float speed; 
    void Start() 
    { 

    } 
    void Update() 
    { 
     Vector2 offset = new Vector2(0, Time.time * speed); 
     GetComponent<Renderer>().material.mainTextureOffset = offset; 
    } 
} 

回答

2

您可以用Time.deltaTimeUpdate功能或協程一個簡單的定時器做到這一點。只需增加你的計時器變量Time.deltaTime,直到它達到你的目標,你的情況是秒。

float timer = 0; 
bool timerReached = false; 
const float TIMER_TIME = 30f; 

public float speed; 

void Update() 
{ 
    if (!timerReached) 
    { 
     timer += Time.deltaTime; 

     Vector2 offset = new Vector2(0, Time.time * speed); 
     GetComponent<Renderer>().material.mainTextureOffset = offset; 
    } 


    if (!timerReached && timer > TIMER_TIME) 
    { 
     Debug.Log("Done waiting"); 

     //Set to false so that We don't run this again 
     timerReached = true; 
    } 
} 
+1

工程就像一個魅力。謝謝 ! – TheNewbie

相關問題