簡體   English   中英

Unity中使用C#的溫度系統

[英]Temperature system in Unity using c#

我希望我的腳本在玩家沖刺(移動)時增加溫度,而在玩家不沖刺時減少溫度。 溫度不得低於溫度(開始沖刺之前),也不得高於特定溫度。 我不能只使用高於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;
        }
    }

而不是使用tempTime來跟蹤短跑的時間,您應該根據是否在短跑來增加或減少與Time.deltaTime成正比的temperature 該溫度應保持在您定義的最小和最大溫度范圍內。

同樣,您的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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM