随时间推移平滑更改lerp值

时间:2018-10-04 21:04:31

标签: c# unity3d time lerp

我目前正在动态的Skybox上工作。我的Skybox由3个单独的脚本组成:

CustomList,CustomListEditor。,TOD.CS

CustomList.cs具有用于存储变量的类,这些变量表示每个变量在特定时间的状态。例如:时间,云层颜色,地平线颜色等。

CustomListEditor.cs是一个自定义检查器,用于设置值并将其添加/删除到“每日时间(TOD)”列表中。

TOD.cs是计算时间传递和允许变量从一个TOD传递到另一个TOD的地方。

我目前遇到的问题:我无法平均收紧每个TOD。基本上,我遇到的问题是我的lerp在一天的每个时间之间运行不平稳,而是运行得慢一些,而运行得更快。我承认这是一个数学问题,我不完全确定如何获得正确的方程式以使其正常工作。

如果有人可以帮助,那将是惊人的。这是我花时间和几天分开的时间。请记住,TOD可以放置在时间上的任何位置,因此示例中的数值不确定。

<!-- language: lang-c# -->

public float TODspeed = 0.02    
     private float currentValue = 0.00f
     public int TODindex = 0;
     public Color horizon;

    void Start()
    {

        GetTarget = new SerializedObject(this.GetComponent<CustomList>());
        ThisList = GetTarget.FindProperty("MyList"); // Find the List in our script and create a refrence of it
        SerializedProperty MyListRef = ThisList.GetArrayElementAtIndex(TODindex);
        SerializedProperty myHorizon = MyListRef.FindPropertyRelative("horizon");
        horizon = myHorizon.colorValue;
    }


    void Update()
    {
        //Grab serialized properties from my List
        //MyListRef is getting a reference of the current TOD

        SerializedProperty MyListRef = ThisList.GetArrayElementAtIndex(TODindex);

        //NextListRef is getting a reference of the next TOD that we will be lerping to.

        SerializedProperty NextListRef = ThisList.GetArrayElementAtIndex(TODindex + 1);
        SerializedProperty myTime = NextListRef.FindPropertyRelative("time");

        //mixTime is supposed to be my equation for the speed of the times of day. I presume that this code is incorrect and I have no idea how to fix it.
        float mixTime = TODspeed * (myTime.floatValue - MyListRef.FindPropertyRelative("time").floatValue);

        //This is where I lerp my TOD variables, so long as CurrentValue ,which is the game time, is less than the next TOD's time value.
        if (currentValue < myTime.floatValue)
        {
            currentValue += (Time.deltaTime*TODspeed);

            horizon = Color.Lerp(horizon, nextHorizon.colorValue, mixTime);
            this.GetComponent<CustomList>().atmosphereGradient.SetColor("_BottomColor", horizon);
        }


        // if game time is greater than my next TOD's time variable, It will compare the TODIndex to what would be the last TOD in the script. If it is smaller than the last TOD it will incriment , If it is bigger or equal to it, it will restart to time of days.
        if (currentValue >= myTime.floatValue)
        {
            int compareValue = ThisList.arraySize - 2;
            if (TODindex < compareValue)
            {
                TODindex++;
            }
            else if (TODindex >= compareValue)
            {
                TODindex = 0;
                currentValue = 0.00f;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您的问题就在这

horizon = Color.Lerp(horizon, nextHorizon.colorValue, mixTime);

您总是在当前值和目标值之间进行插值=>它们之间的差每次都较小=>“衰落”变得越来越慢,时间也越来越慢。

您想要做的是在原始值和目标值之间不断缩小。我看不到声明horizon的位置,但是您应该将原始Color存储在Update之外,例如为startColor,然后将您的行更改为

horizon = Color.Lerp(startColor, nextHorizon.colorValue, mixTime);

注意:我不完全理解您的其余代码,但据我了解,您的问题主要是影响很大,因此我认为其余的工作正常。

相关问题