Search Unity

Cannot seem to get Color.Lerp to work on an image in a canvas.

Discussion in 'Scripting' started by Gotthoms, Dec 30, 2019.

  1. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10
    I've been struggling with this issue for some time now. I simply want a lerp between the alpha of an image (from 0 to 1), but the transition never completes, no matter where I execute the lerp. Here is a simplified version of what I'm trying :



    public Image redUI;
    public bool hit;
    Color alphaZero = new Color(1f, 1f, 1f, 0f);
    Color alpha = new Color(1f, 1f, 1f, 1f);

    void FixedUpdate () {

    if(hit)
    {

    redUI.color = Color.Lerp(alphaZero, alpha, .2f);

    }

    }
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Is hit remaining true the whole time or does it get reset at some point to false?

    Edit: Nevermind, your lerp is only going .2 so it will never complete.
     
  3. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10
    No matter what value I put in the "t" of the lerp no transition ever happens. And the hit boolean stays true for long enough to let the interpolation happen.
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    You need to pass in a changing value of 0 - 1 for t.

    Code (CSharp):
    1. public Image redUI;
    2. public float transTime = 2.0f;
    3. public bool hit;
    4. Color alphaZero = new Color(1f, 1f, 1f, 0f);
    5. float timePassed = 0.0f;
    6.  
    7. void FixedUpdate()
    8. {
    9.      if (hit && timePassed < transTime)
    10.      {
    11.           timePassed += Time.fixedDeltaTime;
    12.           redUI.color = Color.Lerp(alphaZero, Color.white, timePassed / transTime);
    13.      }
    14. }
     
    makaka-org likes this.
  5. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10
    Thank you for the reply, I'll try this out tomorrow. What I find weird is that I've never needed to gradually increase the value of "t" in order for the lerp to work in other situations using Vector3.Lerp or Mathf.Lerp, hence my confusion at first. But I might be wrong, I'll take another look at my documentation.
     
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I have seen tutorials use Lerp wrong that could be the cause of the confusion.

    An example of how not to use lerp:
    Code (CSharp):
    1. transform.position = Vector3.Lerp(transform.position, targetPosition, 0.1f);
    This does "work" but it never really finishes and will continue to lerp almost indefinitely(if we had unlimited precision it would.)
     
  7. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10
    That's interesting. That means I've been using lerp wrong this whole time. Thanks for the explanation.
     
  8. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10

    The bit of code you showed me works perfectly. Thanks for your help!
     
    Chris-Trueman likes this.