Unity Timer变为负值

本文关键字:Timer Unity | 更新日期: 2023-09-27 18:29:26

所以,我正在尝试为我的游戏制作一个"商人"系统。5分钟后(计时器滴答作响)商家将可用,第二个计时器将开始滴答作响,但第二个定时器处于-。

    void Update()
{
    timeremaining -= Time.deltaTime;
    int minutes = Mathf.FloorToInt(timeremaining / 60F);
    int seconds = Mathf.FloorToInt(timeremaining - minutes * 60);
    string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
    howlonghere -= Time.deltaTime;
    int minutes2 = Mathf.FloorToInt(howlonghere / 60F);
    int seconds2 = Mathf.FloorToInt(howlonghere - minutes * 60);
    string niceTime2 = string.Format("{0:0}:{1:00}", minutes, seconds);
    if (timeremaining > 0)
    {
        Merchanthuman.enabled = false;
        Merchanthuman.interactable = false;
        Tiimer.text = "Merchant will be here: " + niceTime;
    }
    else
    {
        Tiimer.text = "Merchant is here for: " + niceTime2;
        Merchanthuman.enabled = true;
        Merchanthuman.interactable = true;
    }
}

"商人来了:"应该会开始新的倒计时,这将是第二次。就像Will在这里是5分钟&在这里是2分钟。

Unity Timer变为负值

让我们稍微拆分一下:

public const float TravelTime = 5.0f;
public const float VisitTime = 4.0f;
public float timeremaining;
public float howlonghere;
//Setup initial timers
void Start()
{
    timeremaining = TravelTime;
    howlonghere = VisitTime;
}
//Check each frame for the scenario
void Update()
{
    if (timeremaining > 0)
    {
        string niceTime = ElapseTravel();
        Merchanthuman.enabled = false;
        Merchanthuman.interactable = false;
        Tiimer.text = "Merchant will be here: " + niceTime;
    }
    else
    {
        string niceTime2 = ElapseVisit();
        Tiimer.text = "Merchant is here for: " + niceTime2;
        Merchanthuman.enabled = true;
        Merchanthuman.interactable = true;
    }
}
//Elapse remaining time when merchant travels
private string ElapseTravel()
{
    timeremaining -= Time.deltaTime;
    int minutes = Mathf.FloorToInt(timeremaining / 60F);
    int seconds = Mathf.FloorToInt(timeremaining - minutes * 60);
    return string.Format("{0:0}:{1:00}", minutes, seconds); 
}
//Elapse stay time when merchant is here
private string ElapseVisit()
{
    howlonghere -= Time.deltaTime;
    int minutes2 = Mathf.FloorToInt(howlonghere / 60F);
    int seconds2 = Mathf.FloorToInt(howlonghere - minutes2 * 60);
    if (howlonghere <= 0)
    {
        timeremaining = TravelTime;
        howlonghere = VisitTime;
    }
    return string.Format("{0:0}:{1:00}", minutes2, seconds2);
}

无论情况如何,您都在降低timeremaininghowlonghere。您需要将这两种情况分开,并根据商人正在前往该地点的事实或他是否已经在这里,只经过(减少)其中一个值。

Unity Timer不是负数。是你自己的变量变为负值。

这里实际发生的情况是,您检查5分钟并执行timeremaining -= Time.deltaTime;,这将不断减少timeremaining变量。

第二件事是你的howlonghere在下降的同时,它变成了负的。因为CCD_ 6小于CCD_。

因此,当你的第一个计时器变成timeremaining <= 0时,你的第二个计时器howlonghere就会变成howlonghere = -3 minutes。(假设最初是timeremaining = 5howlonghere = 2)。

如果你不想改变你的逻辑,你能做什么。

void Update()
{
    timeremaining -= Time.deltaTime;
    int minutes = Mathf.FloorToInt(timeremaining / 60F);
    int seconds = Mathf.FloorToInt(timeremaining - minutes * 60);
    string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
    if (timeremaining > 0)
    {
        Merchanthuman.enabled = false;
        Merchanthuman.interactable = false;
        Tiimer.text = "Merchant will be here: " + niceTime;
    }
    else {
        howlonghere -= Time.deltaTime;
        int minutes2 = Mathf.FloorToInt(howlonghere / 60F);
        int seconds2 = Mathf.FloorToInt(howlonghere - minutes * 60);
        string niceTime2 = string.Format("{0:0}:{1:00}", minutes, seconds);   
        Tiimer.text = "Merchant is here for: " + niceTime2;
        Merchanthuman.enabled = true;
        Merchanthuman.interactable = true; 
    }
}

显然,您会对其进行更多相应的定制。但这只是为了让你明白。