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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Color.lerp not resetting?

Discussion in 'Editor & General Support' started by codejoy, Feb 7, 2018.

  1. codejoy

    codejoy

    Joined:
    Aug 17, 2012
    Posts:
    204
    I have this code in an update that is changing background colors over time:

    Code (CSharp):
    1. void Start()
    2. {
    3.     //setup up current color and next color
    4.     _currentColor = color1;
    5.     _nextColor = color2;
    6. }
    7.  
    8. void Update()
    9. {
    10.         _lerpedColor = Color.Lerp(_currentColor, _nextColor, Time.time*.1f); //001
    11.         cam.backgroundColor = _lerpedColor;
    12.  
    13.         if (_lerpedColor == _nextColor) {
    14.             Debug.Log ("Current Color: " + _currentColor);
    15.             _currentColor = _nextColor;
    16.            _nextColor = someNewColor;
    17.             _lerpedColor = Color.white; //one that wont ever happen
    18.  
    19.         }
    20. }
    This code works great until _lerpedColor == _nextColor it thinks its time to change colors, so it does? but then after that _lerpedColor always equals _nextColor ?? I even reset _lerpedColor. Basically I have a series of colors I want to cycle between then change up the next series rinse repeat.... I cannot figure out why _lerpedColor is always _nextColor when the second time through its like I have to reset the Color.Lerp count?
     
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    "Time.time" is time since the game started. When "_lerpedColor == _nextColor" is true, "Time.time*.1f" is going to be equal or greater than 1f forever from that point on, so it will instantly lerp to _nextColor.

    Keep a timer variable, and add Time.deltaTime to it in Update, and use that instead of Time.time
     
  3. codejoy

    codejoy

    Joined:
    Aug 17, 2012
    Posts:
    204
    Thank you I created a timer value and added deltaTime to it and reset it when appropriate .. works perfect now!