特定时间后如何停止纹理滚动 [英] How to stop Texture scrolling after certain time
问题描述
我下面有这段代码,只需用Quad滚动背景即可.我的问题是一段时间后如何停止背景滚动.例如,我想在到达滚动图像的末尾后,将最后一个可见的块锁定为关卡其余部分的背景.由于我的播放器具有恒定的速度,因此我想到了类似的效果:大约20秒钟后,停止滚动并保持图像可能.我对Unity真的很陌生,我不确定该怎么做,也找不到可行的方法.我会感谢您的帮助!
I have this code below which, just makes a scrolling background with a Quad. My question is how can I stop the scrolling of the background after a certain time. For example, I want after I reach the end of my scrolling image, the last visible piece to be locked as the background for the rest of the level. Since my player has a constant speed I imagined that something like: after maybe 20 seconds, stop scrolling and keep the image would be possible. I am really new to Unity and I am not really sure how to do it nor I found a way that works. I would appreciate the help!
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.deltaTime
和Update
函数的简单计时器或在协程中执行此操作.只需使用Time.deltaTime
递增计时器变量,直到达到您的目标 30 秒.
You can do this with a simple timer with Time.deltaTime
an Update
function or in the a coroutine. Just increment you timer variable with Time.deltaTime
until it reaches your target which is 30 seconds in your case.
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;
}
}
这篇关于特定时间后如何停止纹理滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!