Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question TimeSpan help please.

Discussion in 'Scripting' started by Homicide, Apr 26, 2023.

  1. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    Hey guys. I have been trying to work out a lerp method on a value across a years time. It lerps a shader seasonaly. Im not too great with the maths, and just started using TimeSpan recently, and im having an issue with the second half of the year.

    The first half of the year works fine, but the second half, not so much. Could anyone shed some light on my wrong doings here? Much appreciated.

    Im not sure how to get a correct TimeSpan for the last half a year. I feel like im doing something so simply stupid wrong, but alas, no luck all morning.

    Code (CSharp):
    1.         private void UpdateSeason()
    2.         {
    3.             TimeSpan timeSinceYearStart = TimeSpan.FromDays(0) + TimeSpan.FromDays(currentTime.DayOfYear) + currentTime.TimeOfDay;
    4.  
    5.             if (timeSinceYearStart.Days > 365 / 2)
    6.             {
    7.                 //SECOND HALF OF YEAR
    8.  
    9.                 TimeSpan timeSinceHalfYearStart = TimeSpan.FromDays(365 / 2);
    10.  
    11.                 TimeSpan timeUntilEndOfYear = CalculateTimeDifference(currentTime.TimeOfDay, TimeSpan.FromDays(365));
    12.                 double percentage = timeSinceHalfYearStart.TotalMinutes / timeUntilEndOfYear.TotalMinutes;
    13.  
    14.                 snowManager.seasonValue = Mathf.Lerp(0, 4, (float) percentage);
    15.             }
    16.             else
    17.             {
    18.                 // FIRST HALF OF YEAR
    19.  
    20.                 TimeSpan timeUntilHalfYear = CalculateTimeDifference(currentTime.TimeOfDay, TimeSpan.FromDays(365) / 2);
    21.                 double percentage = timeSinceYearStart.TotalMinutes / timeUntilHalfYear.TotalMinutes;
    22.  
    23.                 snowManager.seasonValue = Mathf.Lerp(4, 0, (float)percentage);
    24.             }
    25.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,283
    What I see above seems at least superficially close to correct so it's likely only some small adjustments.

    It looks like you want snow value to go from 0 to 4 as the year goes from 0 to half

    Then you want it to go from 4 back to 0 as the year goes from half to full?

    Gotta unpack some of that, find out what those values are going into your computations. Put 'em in local variables, start dumping them to Debug.Log(), see what's going on.

    I would even separate the "get now" from the computations so you can feed in a number from 0 to 365 and have it work right, then get that 0 to 365 number from DateTime as a separate secondary problem.
     
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638

    Ahh yea, i had just a wee little smidge out of place. Working now, thanks dude. Just gettin 'mad as a hatter there"... too many hours this morning on that one method. Feels like a monday.
     
    Kurt-Dekker likes this.