Quaternion to Vector3 jumping around

So I’ve been trying to create a script that would take the current time, convert it to seconds, and take the current day with an algorithm to get the accurate sunset for that day, and set the x value of the directional sunlight to the corresponding output.

What’s appearing to happen is that whenever Rotation > 90, the light y value jitters between Rotation and 180 - Rotation

Here’s the code that I have currently

public float Time;
    public int Day;
    public float Slope;
    public float Intercept;
    public float Rotation;

    private void Update() {
        string daySetting = functions.GetSetting("gameTime").SettingData;
        Debug.Log("setting = " + daySetting);
        if (daySetting == "worldTime") {
            //Commented out for testing purposes, this IS part of the final script
            //Time = (System.DateTime.Now.Hour * 3600) + (System.DateTime.Now.Minute * 60) + (System.DateTime.Now.Second);
            Day = System.DateTime.Now.DayOfYear;
            // y = -81.5 | x - 265 | + 64800
            float sunset = -81.5f * Mathf.Abs(Day - 265) + 64800;


            //Calculating Equation
            Slope = 0.0f;
            Intercept = 0.0f;

            Slope = 180 / (sunset - 21600);
            Intercept = -1 * 21600 * Slope;

            Rotation = (Slope * Time) + Intercept;
                        
            transform.rotation = Quaternion.Euler(Rotation, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
        }
    }

I don’t know what’s going on, or what might cause this, but any and all help on why this may be happening, or code that would help fix this issue is greatly appreciated.

This is because unity stores rotation in Quaternions, but you are using euler angles. When working with quaternions unity treats -190 degrees around axis as just +170 degres around axis;

Quaternions are very nice to avoid gimbal lock issue, - search on youtube for explanations

Back to your problem, there is an easy fix - never sample the transform.rotation.euler angles, just store your degrees in a variable, then update it every frame; you can then set transform.rotation using Quaternion.Euler(), just like you do right now

So basically, - keep everything as is, except don’t sample transform.rotation.eulerAngles, because unity doesn’t keep track of Euler degrees. It reconstructs them as needed, and at that point it won’t remember about extra revolutions (190 -to- 170 example above). It will always reconstruct taking the shortest path