【问题标题】:Unity Timer goes into negative?Unity Timer 变为负数?
【发布时间】:2016-04-11 03:56:02
【问题描述】:

所以,我正在尝试为我的游戏制作一个“商人”系统。 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 be here is 5 分钟 & is here for 2 分钟一样。

【问题讨论】:

  • 那么,问题是什么?发生什么了?应该发生什么?
  • 另外,看起来deltaTime 不是恒定的,所以timeremaining 当然可以而且很可能最终会是负数。
  • “商家来这里是为了:”应该开始新的倒计时,这将是第二次。就像 Will be here is 5 minutes & is here for is 2 minutes.
  • 那么,howlonghere 设置在哪里?即使timereamining>0,您似乎也在减少howlonghere。所以根据这两者的相对值,当timeremaining 达到<=0 时,howlonghere 可能已经是负数了。
  • 注意Unity游戏软件的标签是unity3d

标签: c# unity3d timer unity5


【解决方案1】:

让我们稍微拆分一下:

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。您需要拆分这两种情况,并根据商家正在前往某个地点或他是否已经在这里的事实,仅经过(减少)其中一个值。

【讨论】:

  • 谢谢,但 niceTime 和 niceTime2 在当前上下文中不存在
  • 啊,我的错。现在应该没事了。
  • 顺便说一句,在“商家”完成访问后,它应该回到商家“再次”访问时的状态
  • 我已经更新了我的答案。希望我的理解正确 - 您希望商家旅行 5 次,然后访问 4 次,这样的场景一遍又一遍?
【解决方案2】:

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

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

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

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

}

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

【讨论】:

    猜你喜欢
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2017-10-22
    • 2021-11-23
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多