Search Unity

Can't update the old position with current one

Discussion in 'Scripting' started by kaimelis, Sep 1, 2016.

  1. kaimelis

    kaimelis

    Joined:
    Apr 28, 2016
    Posts:
    18
    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.

    Code (CSharp):
    1.  [Header("Object reference")]
    2.     public Transform stars;
    3.  
    4.     [Header("Color changers")]
    5.     public Gradient nightDayColor;
    6.     public Gradient nightDayFogColor;
    7.     public AnimationCurve fogDensityCurve;
    8.  
    9.     [Header("Current time of day")]
    10.     public float maxIntensity = 3f;
    11.     public float minIntensity = 0f;
    12.     public float minPoint = -0.2f;
    13.     public Vector3 dayRotateSpeed;
    14.     public Vector3 nightRotateSpeed;
    15.     private Light mainLight;
    16.     private Skybox sky;
    17.     private Material _sky;
    18.     public float fogScale = 1f;
    19.  
    20.    [Header("Sun parameters")]
    21.     [SerializeField]
    22.     private float TimeofDay = 1;
    23.     [SerializeField]
    24.     private float amplitudeX = 2.0f;
    25.     [SerializeField]
    26.     private float amplitudeZ = 3.0f;
    27.     [SerializeField]
    28.     private float periodInSec = 120;
    29.     [SerializeField]
    30.     private float frequency = 2.0f;
    31.  
    32.     private float twoPi = Mathf.PI * 2f;
    33.     [SerializeField]
    34.     private bool night = false;
    35.     [SerializeField]
    36.     private bool day = false;
    37.  
    38.     public Vector3 oldPosition = new Vector3();
    39.     public Vector3 sunposition;
    40.     void Start()
    41.     {
    42.         mainLight = GetComponent<Light>();
    43.         _sky = RenderSettings.skybox;
    44.         frequency = 1 / periodInSec;
    45.         sunposition = transform.localPosition;
    46.     }
    47.  
    48.  
    49.     void Update()
    50.     {
    51.         stars.transform.rotation = transform.rotation;//rotate starts with sun
    52.  
    53.         float tRange = 1 - minPoint; //total day time
    54.         float dot = Mathf.Clamp01((Vector3.Dot(mainLight.transform.forward, Vector3.down) - minPoint) / tRange);// from -1 to 1
    55.         float i = ((maxIntensity - minIntensity) * dot) + minIntensity;
    56.         float speed = Time.deltaTime*TimeofDay;
    57.         mainLight.intensity = i;
    58.  
    59.         mainLight.color = nightDayColor.Evaluate(dot);//at particular point give excatcly that color :)
    60.         RenderSettings.ambientLight = mainLight.color;
    61.  
    62.         RenderSettings.fogColor = nightDayFogColor.Evaluate(dot);
    63.         RenderSettings.fogDensity = fogDensityCurve.Evaluate(dot) * fogScale;
    64.  
    65.  
    66.         float x = amplitudeX*Mathf.Cos(twoPi*Time.time*frequency);
    67.         float z = amplitudeZ*Mathf.Sin(twoPi*Time.time*frequency);
    68.  
    69.  
    70.         //day
    71.         if (dot > 0)
    72.         {
    73.             day = true;
    74.             night = false;
    75.             transform.Rotate(dayRotateSpeed*speed);
    76.            
    77.  
    78.         }
    79.         //night
    80.         else
    81.         {
    82.             transform.Rotate(nightRotateSpeed * speed);
    83.             day = false;
    84.             night = true;
    85.  
    86.         }
    87.        // transform.localPosition = new Vector3(Mathf.Cos(speed), 0, Mathf.Sin(speed));
    88.  
    89.         if (day && !night)
    90.         {
    91.  
    92.                 sunposition = new Vector3(-x, z, 0);
    93.  
    94.         }
    95.        
    96.         else if (night && !day)
    97.         {
    98.             x = -60;
    99.             z = -20;
    100.            oldPosition = transform.localPosition = new Vector3(-60,-20,0);
    101.         }
    102.     }
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    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?
     
  3. kaimelis

    kaimelis

    Joined:
    Apr 28, 2016
    Posts:
    18
    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
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    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?
     
  5. kaimelis

    kaimelis

    Joined:
    Apr 28, 2016
    Posts:
    18
    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.