Search Unity

Question Using Color.Lerp ( bug?)

Discussion in 'Scripting' started by UlrickEvrard3K, Jun 8, 2021.

  1. UlrickEvrard3K

    UlrickEvrard3K

    Joined:
    May 15, 2021
    Posts:
    7
    Hello, I'm currently coding a script that mays change an image color, using color.lerp where t is based on a car speed ( that I report between 0 and 1 using mathematixxxxx ), but the image only goes to b without passing in the steps :

    Code (CSharp):
    1.  void Update()
    2.     {
    3.         if (player.speed > 80)
    4.         {
    5.             currentColor = Color.LerpUnclamped(currentColor, desiredColor, ((player.speed-80)/10));
    6.             Debug.Log((player.speed - 80) / 10); // It reports 0 when player speed is 80
    7.             // and 1 when it's 90.
    8.             graphic.color = currentColor;
    9.         }
    10.     }
    I tried Color.LerpUnclamped and it did the same thing, any idea would be welcome !

    Thank you.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Is
    player.speed
    an integer? If so it is doing integer calculations, and 89 / 90 is 0 with integer calculations.

    If this is a case, use a floating point constant somewhere in there to cause it to use floating point calculation, like so:

    Code (csharp):
    1. float fraction = (player.speed - 80.0f) / 10;
    And then use
    fraction
    as the third term to lerp.

    Generally you should strive to avoid long hairy lines like line 5 above and instead do lots of small calculations and put them in temporary variables, so you can print them and reason about them.
     
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    currentColor = Color.Lerp(minSpeedColor, maxSpeedColor, player.speed / maxSpeed));
     
  4. UlrickEvrard3K

    UlrickEvrard3K

    Joined:
    May 15, 2021
    Posts:
    7
    Thanks you for reply, player.speed is already a float, I tried to put my calculation in a variable as you adviced, it doesnt' work but ty for the tip anyway.
     
  5. UlrickEvrard3K

    UlrickEvrard3K

    Joined:
    May 15, 2021
    Posts:
    7
    Thanks you for reply too, but " player.speed / maxSpeed " doesn't give me the numbers i need to.
     
  6. UlrickEvrard3K

    UlrickEvrard3K

    Joined:
    May 15, 2021
    Posts:
    7
    I cheated a little bit by adding another condition with the inverse Color.Lerp, does the work for me but why didn't work is still a question.

    Here is the code if someone needs it one day :
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (player.speed > 80)
    4.         {
    5.             currentColor = Color.Lerp(currentColor, desiredColor, Mathf.PingPong(Time.deltaTime,1));
    6.             Debug.Log(fraction);
    7.             graphic.color = currentColor;
    8.          
    9.         }
    10.         else if (player.speed < 80)
    11.         {
    12.             currentColor = Color.Lerp(currentColor, Color.white, Mathf.PingPong(Time.deltaTime, 1));
    13.             graphic.color = currentColor;
    14.         }
    15.     }
     
  7. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    What numbers do you need ?
    Your solution above is not very good.