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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Random Color Changing with Lerp not working

Discussion in 'Scripting' started by MrMikpik, Sep 24, 2021.

  1. MrMikpik

    MrMikpik

    Joined:
    Dec 11, 2019
    Posts:
    10
    I've been trying to change the camera background constantly with random colors, transitioning smoothly between them. My idea is: at the start of the game 3 colors are created, and there are 3 cycles. It starts with cycle 1, where it does a Lerp between newColor1 and newColor2, in cycle 2 between color 2 and 3, and in the end in cycle 3 between color 3 and 1, and (not yet implemented) creating each time new colors through cycles, so, in cycle 1 it creates a newColor3, in cycle 2 a newColor1 and in cycle 3 a new color 2. So the problem is, the colors keep changing as planned, but, they do without doing the Lerp, so they don't change smoothly.

    Here's the code(don't mind ColorChange() )
    I tried both doing all in the update method and by creating 3 separate functions (ColorLerp1, 2, 3)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ColorChanging : MonoBehaviour
    6. {
    7.     private float gameTime;
    8.     private float cycleTime = 3f;
    9.  
    10.     public Camera cam;
    11.  
    12.     private Color newColor1;
    13.     private Color newColor2;
    14.     private Color newColor3;
    15.     public float t = 3.0f;
    16.  
    17.     private void Start()
    18.     {
    19.  
    20.         newColor1 = new Color
    21.            (
    22.             Random.value,
    23.             Random.value,
    24.             Random.value
    25.             );
    26.  
    27.         cam.backgroundColor = newColor1;
    28.         PlayerPrefs.SetInt("ColorCycle", 1);
    29.  
    30.         newColor2 = new Color
    31.            (
    32.            Random.value,
    33.            Random.value,
    34.            Random.value
    35.            );
    36.         newColor3 = new Color
    37.            (
    38.            Random.value,
    39.            Random.value,
    40.            Random.value
    41.            );
    42.     }
    43.  
    44.     private void ChangeColor()
    45.     {
    46.         newColor1 = new Color
    47.                 (
    48.                   Random.value,
    49.                   Random.value,
    50.                   Random.value
    51.                 );
    52.     }
    53.  
    54.     private void ColorLerp1()
    55.     {
    56.         cycleTime = gameTime + 3f;
    57.         cam.backgroundColor = Color.Lerp(newColor1, newColor2, t);
    58.     }
    59.  
    60.     private void ColorLerp2()
    61.     {
    62.         cycleTime = gameTime + 3f;
    63.         cam.backgroundColor = Color.Lerp(newColor2, newColor3, t);
    64.     }
    65.  
    66.     private void ColorLerp3()
    67.     {
    68.         cycleTime = gameTime + 3f;
    69.         cam.backgroundColor = Color.Lerp(newColor3, newColor1, t);
    70.     }
    71.  
    72.     private void Update()
    73.     {
    74.         gameTime = Time.timeSinceLevelLoad;
    75.  
    76.         if (gameTime > cycleTime && PlayerPrefs.GetInt("ColorCycle") == 1)
    77.         {
    78.             ColorLerp1();
    79.  
    80.             PlayerPrefs.SetInt("ColorCycle", 2);
    81.         }
    82.  
    83.         if (gameTime > cycleTime && PlayerPrefs.GetInt("ColorCycle") == 2)
    84.         {
    85.             ColorLerp2();
    86.  
    87.             PlayerPrefs.SetInt("ColorCycle", 3);
    88.         }
    89.  
    90.         if (gameTime > cycleTime && PlayerPrefs.GetInt("ColorCycle") == 3)
    91.         {
    92.             ColorLerp3();
    93.  
    94.             PlayerPrefs.SetInt("ColorCycle", 1);
    95.         }
    96.  
    97.     }
    98. }
    99.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    That's a lot of code... I'm not even really sure what's happening there.

    This is my go-to smooth-transition pattern, works for pretty much any continuous quantity:

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

    I would also advise staying away from splattering crap like the above all over your code. Keep it tidy in one place to avoid typos and visual noise when reading the code.

    Here's an example of simple persistent loading/saving values using PlayerPrefs:

    https://gist.github.com/kurtdekker/01da815d2dfd336a925ae38019c3a163

    Useful for a relatively small number of simple values.
     
  3. MrMikpik

    MrMikpik

    Joined:
    Dec 11, 2019
    Posts:
    10

    Unluckly I couldn't find use in your code, but, on the other hand I managed to get a solution anyways, I'm gonna post it for whoever was interested in the topic:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ColorChangingV2 : MonoBehaviour
    6. {
    7.     public Color color1;
    8.     public Color color2;
    9.     private float duration = 6f;
    10.  
    11.     private float gameTime;
    12.     private float cycleTime;
    13.  
    14.     public Camera cam;
    15.  
    16.     void Start()
    17.     {
    18.         cam = GetComponent<Camera>();
    19.         cam.clearFlags = CameraClearFlags.SolidColor;
    20.  
    21.         color1 = new Color
    22.            (
    23.            Random.Range(100,220),
    24.            Random.value,
    25.            Random.value
    26.            );
    27.  
    28.         color2 = new Color
    29.            (
    30.            Random.Range(100, 220),
    31.            Random.value,
    32.            Random.value
    33.            );
    34.  
    35.         cycleTime = 6f;
    36.  
    37.         PlayerPrefs.SetInt("ColorCycle", 1);
    38.     }
    39.  
    40.     void RandomColor1()
    41.     {
    42.         color1 = new Color
    43.            (
    44.            Random.Range(100, 220),
    45.            Random.value,
    46.            Random.value
    47.            );
    48.  
    49.         PlayerPrefs.SetInt("ColorCycle", 2);
    50.     }
    51.  
    52.     void RandomColor2()
    53.     {
    54.         color2 = new Color
    55.            (
    56.            Random.Range(100, 220),
    57.            Random.value,
    58.            Random.value
    59.            );
    60.  
    61.         PlayerPrefs.SetInt("ColorCycle", 1);
    62.     }
    63.  
    64.     void Update()
    65.     {
    66.         gameTime = Time.timeSinceLevelLoad;
    67.  
    68.         float t = Mathf.PingPong(Time.time, duration) / duration;
    69.         cam.backgroundColor = Color.Lerp(color1, color2, t);
    70.  
    71.         if (gameTime > cycleTime && PlayerPrefs.GetInt("ColorCycle") == 1)
    72.         {
    73.             cycleTime = gameTime + 6f;
    74.             RandomColor1();
    75.         }
    76.  
    77.         if (gameTime > cycleTime && PlayerPrefs.GetInt("ColorCycle") == 2)
    78.         {
    79.             cycleTime = gameTime + 6f;
    80.             RandomColor2();
    81.         }
    82.     }
    83. }
    Note: the Random.Range could be a Random.value aswell, it's just that I had a personal preference in how bright was the color.

    P.S. thanks anyways for the help provided
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You're using the Color class wrong and you are not getting random colors the way you think you are. The Color constructor takes numbers between 0 and 1, NOT 0 to 255 as you are using it. Each and every one of your colors there is going to have 100% in the red channel.

    I have no idea why you're using PlayerPrefs in here. Just set an int variable.
     
  5. MrMikpik

    MrMikpik

    Joined:
    Dec 11, 2019
    Posts:
    10
    Yeah that may seem confusing but it's because I want the player to work with it later

    Oh yeah, that makes absolute sense, it was very dumb by me to not think about it. Thanks for the tip!!