C# 用c语言实现统一温度系统#

C# 用c语言实现统一温度系统#,c#,unity3d,temperature,C#,Unity3d,Temperature,我想让我的脚本在玩家冲刺(轮班)时增加温度,在他不冲刺时减少温度。温度不能低于(他开始短跑之前)的温度,也不能高于某一点。我不能只使用高于x或低于x的,因为短跑并不是影响它的全部 希望我解释得不好。。。 我以为会有用?我做错了什么 代码如下: float tempTime; public int temperature = 32; if (Input.GetKeyDown(KeyCode.LeftShift) && temperature == originalTemp

我想让我的脚本在玩家冲刺(轮班)时增加温度,在他不冲刺时减少温度。温度不能低于(他开始短跑之前)的温度,也不能高于某一点。我不能只使用高于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;
        }
    }
float-time;
公共室内温度=32;
if(Input.GetKeyDown(KeyCode.LeftShift)和&temperature==originalTemp)
原始温度=温度;
if(Input.GetKeyUp(KeyCode.LeftShift)和Input.GetKeyUp(KeyCode.W)和&temperature==originalTemp)
原始温度=温度;
if(Input.GetKey(KeyCode.LeftShift))
{
如果(试探时间>=5f&&T温度=originalTemp)
{
温度++;
TENTIME=0;
}否则{
TENTIME+=Time.deltaTime;
}
}如果(!Input.GetKey(KeyCode.W)){
如果(试探时间>=5f&&T温度=originalTemp)
{
温度--;
TENTIME=0;
}否则{
TENTIME+=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;
        }
    }
}