Can't update the old position with current one

Hi there. I am making day/night cycle. I am rotating the directional light and then i draw halo and trying to move it with sin and cos. It works just one problem i have is when it’s night i want the sun to move faster or to be on exact position and then update that position with cos and sin. But when i do that. It only start working again old position not updated position.

 [Header("Object reference")]
    public Transform stars;

    [Header("Color changers")]
    public Gradient nightDayColor;
    public Gradient nightDayFogColor;
    public AnimationCurve fogDensityCurve;

    [Header("Current time of day")]
    public float maxIntensity = 3f;
    public float minIntensity = 0f;
    public float minPoint = -0.2f;
    public Vector3 dayRotateSpeed;
    public Vector3 nightRotateSpeed;
    private Light mainLight;
    private Skybox sky;
    private Material _sky;
    public float fogScale = 1f;

   [Header("Sun parameters")]
    [SerializeField]
    private float TimeofDay = 1;
    [SerializeField]
    private float amplitudeX = 2.0f;
    [SerializeField]
    private float amplitudeZ = 3.0f;
    [SerializeField]
    private float periodInSec = 120;
    [SerializeField]
    private float frequency = 2.0f;

    private float twoPi = Mathf.PI * 2f;
    [SerializeField]
    private bool night = false;
    [SerializeField]
    private bool day = false;

    public Vector3 oldPosition = new Vector3();
    public Vector3 sunposition;
    void Start()
    {
        mainLight = GetComponent<Light>();
        _sky = RenderSettings.skybox;
        frequency = 1 / periodInSec;
        sunposition = transform.localPosition;
    }


    void Update()
    {
        stars.transform.rotation = transform.rotation;//rotate starts with sun

        float tRange = 1 - minPoint; //total day time
        float dot = Mathf.Clamp01((Vector3.Dot(mainLight.transform.forward, Vector3.down) - minPoint) / tRange);// from -1 to 1
        float i = ((maxIntensity - minIntensity) * dot) + minIntensity;
        float speed = Time.deltaTime*TimeofDay;
        mainLight.intensity = i;

        mainLight.color = nightDayColor.Evaluate(dot);//at particular point give excatcly that color :)
        RenderSettings.ambientLight = mainLight.color;

        RenderSettings.fogColor = nightDayFogColor.Evaluate(dot);
        RenderSettings.fogDensity = fogDensityCurve.Evaluate(dot) * fogScale;


        float x = amplitudeX*Mathf.Cos(twoPi*Time.time*frequency);
        float z = amplitudeZ*Mathf.Sin(twoPi*Time.time*frequency);


        //day
        if (dot > 0)
        {
            day = true;
            night = false;
            transform.Rotate(dayRotateSpeed*speed);
           

        }
        //night
        else
        {
            transform.Rotate(nightRotateSpeed * speed);
            day = false;
            night = true;

        }
       // transform.localPosition = new Vector3(Mathf.Cos(speed), 0, Mathf.Sin(speed));

        if (day && !night)
        {

                sunposition = new Vector3(-x, z, 0);

        }
       
        else if (night && !day)
        {
            x = -60;
            z = -20;
           oldPosition = transform.localPosition = new Vector3(-60,-20,0);
        }
    }

Your code moves the sun position by the Cos(x) and Sin(z). But at night your just setting it to a constant value. Is this not what you want? I assume -60,-20 is somewhere off screen for night time?

Yeah i am putting in the constant position because with speed it didn’t work. So I was like, ok if i put it in -60, -20 then it will start moving from that position when it’s day. But it doesn’t do that. it just starts where it ended before night started

Thats because the very first cycle of the day, just gets x,z values from the Sin/Cos functions above. It doesn’t have any knowledge of what happened at night. Also I notice during the day your setting a variable called sunposition. But you never actually use that sunposition anywhere that I can see? Are you suppose to be setting the transform based on that SunPosition?

I was trying stuff out so accidentally left that sunposition in the code. I just wanted to assign transform.localposition to a variable. But thanks. I got it how to fix it.