1

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;
            }
        }
    }
  • 2
    Just saying that you're "unable" to do something isn't really a question that can be answered; can you be more specific about the observed behaviour, the expected behaviour, and what you're having problems with? – Eric Lippert Oct 04 '18 at 21:06
  • My suggestion to you, admittedly not understanding your problem, is that you break it down into smaller problems and make for each one a **method** that solves that problem. For example: can you make a method that takes a sorted list of floats between 0 and 1, and a target, and returns an integer saying which interval the float fell into? If you can solve that problem **by itself** then you have code you can test, code you can verify, and code you can optimize, and now you have a tool that your lerping code can use. – Eric Lippert Oct 04 '18 at 21:09
  • Is there any very speical reason why you are working with `SerializedProperty` in a normal `MonoBehaviour` ?? – derHugo Oct 05 '18 at 05:48
  • Thanks guys, I will edit the post as I was rushed when posting it and had thought I had put enough info. @der – David Klein Oct 06 '18 at 14:30

1 Answers1

0

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 e.g. 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.

derHugo
  • 83,094
  • 9
  • 75
  • 115