简体   繁体   中英

Change lerp values smoothly over time

I am currently working on a dynamic skybox. My skybox is composed of 3 separate scripts:

CustomList ,CustomListEditor., TOD.CS

CustomList.cs has class that stores variables for what each variable will be at a specific time. Example: time, cloud color horizon color ect.

CustomListEditor.cs is a custom inspector to set values and Add/Remove them to a list of Times of Day (TOD).

TOD.cs is where am calculating time passing and lerping variables from one TOD to another.

The problem I am currently having: I am unable to evenly lerp each TOD. basically the problem I am having is that my lerp is not running smoothly between each Time of Day and is instead having portions that run slower and some that run faster. I Acknowledge this is a Math problem and I am not entirely sure how to go about getting the correct equation to make this work properly.

if anyone could help that would be amazing. Here is an i drew up of time and the separate time of days. keep in mind the TOD's could be placed anywhere in time so the number values are not definite in the example.

<!-- 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;
            }
        }
    }

Your problem is in the line

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

you allways interpolate between the current value and the target value => the difference between those is everytime smaller => the "fading" gets slower an slower in time.

What you want to do instead is a constant fading between the original and the target value. I don't see where horizon is declared but you should instead store the original Color outside of Update eg as startColor and than change your line to

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

Note: I don't completely understand the rest of your code but as I understood your problem was mostly the lerping effect so I assumed that the rest works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM