我希望我的脚本在玩家冲刺(换档)时增加温度,并在他没有时减去。温度不得低于(在他开始短跑之前),也不得高于某一点。我不能只使用高于x或低于x的原因,因为冲刺不会影响它......
希望我没有解释得不好...... 我觉得它会起作用吗?我做错了什么?
以下是代码:
float tempTime;
public int temperature = 32;
if (Input.GetKeyDown(KeyCode.LeftShift) && temperature == originalTemp)
originalTemp = temperature;
if (Input.GetKeyUp(KeyCode.LeftShift) && Input.GetKeyUp(KeyCode.W) && temperature == originalTemp)
originalTemp = temperature;
if (Input.GetKey(KeyCode.LeftShift))
{
if (tempTime >= 5f && temperature <= originalTemp * 1.25f && temperature >= originalTemp)
{
temperature++;
tempTime = 0;
} else {
tempTime += Time.deltaTime;
}
} else if (!Input.GetKey(KeyCode.W)) {
if (tempTime >= 5f && temperature <= originalTemp * 1.25f && temperature >= originalTemp)
{
temperature--;
tempTime = 0;
} else {
tempTime += Time.deltaTime;
}
}
答案 0 :(得分:0)
而不是tempTime
来跟踪短跑的时间长短,你应该根据你是或否来增加或减少与temperature
成比例的Time.deltaTime
没有冲刺。此温度应保持在您定义的最小和最大温度范围内。
此外,您的originalTemp = temperature
行/ if
语句按原样无用,因为只有在值已经相等的情况下才会执行该组。你无论如何都不需要知道原始的,只需要最小(静止)和最大(过热)温度。
这可以让你走上正轨:
public float minTemperature = 32;
public float temperature = 32;
public float maxTemperature = 105;
// this defines both heatup and cooldown speeds; this is 30 units per second
// you could separate these speeds if you prefer
public float temperatureFactor = 30;
public bool overheating;
void Update()
{
float tempChange = Time.deltaTime * temperatureFactor;
if (Input.GetKey(KeyCode.LeftShift) && !overheating)
{
temperature = Math.Min(maxTemperature, temperature + tempChange);
if (temperature == maxTemperature)
{
overheating = true;
// overheating, no sprinting allowed until you cool down
// you don't have to have this in your app, just an example
}
} else if (!Input.GetKey(KeyCode.W)) {
temperature = Math.Max(minTemperature, temperature - tempChange);
if (temperature == minTemperature)
{
overheating = false;
}
}
}