Unity Timer进入负面?

时间:2016-01-06 22:00:35

标签: c# unity3d timer unity5

所以,我正在尝试为我的游戏制作一个“商家”系统。 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;
    }
}

“商人在这里:”应该开始新的倒计时,这将是第二次。就像意志在这里是5分钟&在这里是2分钟。

2 个答案:

答案 0 :(得分:0)

让我们把它分开一点:

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。您需要拆分两个方案并且仅过去(减少)其中一个值,具体取决于商家前往位置或者他已经在这里的事实。

答案 1 :(得分:0)

Unity Timer不是负面的。你自己的变量是负面的。

这里实际发生的是你正在检查5分钟并执行timeremaining -= Time.deltaTime;,这会不断减少你的timeremaining变量。

第二件事是你正在减少howlonghere,同时它会变为负面。因为howlonghere小于timeremaining

因此,当您的第一个计时器变为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; 
    }

}

显然,您会相应地对其进行更多自定义。但这只是为了让你清楚。