当循环不工作时,代码没有运行60帧

本文关键字:运行 60帧 代码 循环 工作 | 更新日期: 2023-09-27 18:07:29

我正试图工作这个简单的代码出来,我需要这个代码在一个连续的循环中运行5秒,而不把它放入更新。

void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
    timer += Time.deltaTime;
    if (other.gameObject.CompareTag("IcePickup"))
    {
        Destroy(other.gameObject);
        Blade1 = GameObject.Find("Blade1");
        Blade1.gameObject.GetComponent<SpriteRenderer>().sprite = IceBlade;
        int counting = 1;
        while (counting <= 10)
        {
            print("icey");
            (Blade1.transform.Rotate(new Vector3(0, 0, -0.50f)); 
            counting ++; 
        }
   }
}

当循环不工作时,代码没有运行60帧

好吧,也许这个例子可以帮助你:

using System;
class Program
{
    private static void Main(string[] args)
    {
        DateTime StartTime = DateTime.Now;
        int Seconds = 5;
        int Counter = 0;
        while(DateTime.Now - StartTime < TimeSpan.FromSeconds(Seconds))
        {
            //Your Code here?
            Counter++;
        }
        Console.WriteLine("In " + Seconds + "seconds are " + Counter + "iterations");
    }
}

问候!

不确定我是否理解这个问题-但是你的编码循环计数到10。10次迭代,而不是秒。这10次迭代可能需要3毫秒或一年或40纳秒,这取决于函数的速度。

有可能所有10次迭代都是在第一帧显示之前完成的,这可能解释了您的问题。

你不能简单地编码一个while这么多秒;你需要质疑计时器装置并自己处理这个标准。或者你尝试越来越大的迭代计数,直到你找到一个大约需要5秒的数字。