[英]Unity Timer goes into negative?
因此,我正在尝试为我的游戏制作一个“商人”系统。 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分钟。
让我们将其拆分一下:
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);
}
无论情况如何,您都在减少timeremaining
和howlonghere
。 您需要拆分两种情况,并根据商人要去的地点或他是否已经在这里的事实,只经过(减少)一个值。
Unity Timer不是负数。 是您自己的变量变为负数。
实际上,您正在检查5分钟并执行timeremaining -= Time.deltaTime;
这将不断减少您的timeremaining
变量。
第二件事是您在减少howlonghere
的同时减少了多长时间。 因为这里howlonghere
的时间少于timeremaining
。
因此,当您的第一个计时器timeremaining <= 0
您的第二个计时器howlonghere
将变为howlonghere = -3 minutes
。 (假设timeremaining = 5
而最初的howlonghere = 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;
}
}
显然,您将对其进行相应的自定义。 但这只是为了让您清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.