[英]How to cycle a coroutine every 10 seconds to accelerate a scrolling background?
我绝对是Unity的新手,我正在尝试创建一个2D游戏,该游戏要求滚动背景每10秒加速一次。 我无法使代码正常工作
我试图建立一个协程,但是似乎每隔一帧而不是每隔10秒调用一次函数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollingBackground : MonoBehaviour
{
public float bgSpeed = 5;
public Renderer bgRend;
public float increment = 2f;
private void Start()
{
StartCoroutine(Accelerate());
}
void Update()
{
bgRend.material.mainTextureOffset += new Vector2(0f, bgSpeed * Time.deltaTime);
StartCoroutine(Accelerate());
}
private IEnumerator Accelerate()
{
while (true)
{
bgSpeed = increment * Time.deltaTime;
yield return new WaitForSeconds(10f);
Debug.Log("Getting Faster!");
Debug.Log("OnCoroutine: " + (int)Time.time);
}
}
}
不仅背景速度非常缓慢(只能达到0.3左右并卡住),而且我似乎也无法完成这项工作。 谢谢你的帮助 !
它在每一帧都调用函数,因为您在Update
内启动了协程。 请参阅更新脚本参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.