如何在一定时间后停止纹理滚动

3
我有以下代码,它使用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;
    }
}
1个回答

2

您可以通过简单的计时器,使用Time.deltaTimeUpdate函数或协程来实现。只需使用Time.deltaTime递增计时器变量,直到达到您的目标时间,例如在您的情况下是30秒。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接