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

How to stop Texture scrolling after certain time

我在下面有这段代码,只是用四边形制作了一个滚动背景。我的问题是如何在一定时间后停止背景的滚动。例如,我希望在到达滚动图像的末尾后,将最后一个可见部分锁定为关卡其余部分的背景。由于我的播放器具有恒定的速度,我想象类似的事情:可能在 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;
    }
}

您可以使用带有 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;
    }
}