Unity Timer 变为负值?
Unity Timer goes into negative?
所以,我正在尝试为我的游戏制作一个 "Merchant" 系统。 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;
}
}
那 "Merchant is here for: " 应该开始新的倒计时,这将是第二次。像 Will be here 是 5 分钟 & is here for 是 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;
}
}
显然你会更相应地定制它。但这只是为了让你清楚。
所以,我正在尝试为我的游戏制作一个 "Merchant" 系统。 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;
}
}
那 "Merchant is here for: " 应该开始新的倒计时,这将是第二次。像 Will be here 是 5 分钟 & is here for 是 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;
}
}
显然你会更相应地定制它。但这只是为了让你清楚。