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

标签 c# unity3d

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

关于c# - 如何在一定时间后停止纹理滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46760391/

相关文章:

c# - TableLayoutPanel 显示垂直滚动

unity3d - Unity - NavMesh 不会烘烤楼梯

c# - 在 Unity3D 中创建 Android XMPP 聊天客户端的最佳方式

用于多种 NPC 类型的 C# FSM

c# - 在多个属性上使用 except 过滤列表

c#.NET USB 设备持久标识符

c# - 要在 C# 中使用的 PInvoke C++ 函数

c# - 如何在C#Nest中进行双重嵌套查询

c# - Unity3D Slerp旋转速度

c# - 如何在 Windows Phone 上统一减少 WebCameraTexture 使用的内存?