2017-10-15 63 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

相关问题