- Home /
Color.Lerp not lerping
I'm trying to lerp the alpha channel of a sprite renderer but it isn't lerping, it just changes the alpha value so it instantly disappears instead of fading, I've searched everywhere but I can't find a solution, here's my code, please help.
(c#)
using UnityEngine;
using System.Collections;
public class MagicWalk : MonoBehaviour
{
Color fullAlpha = new Color (1f, 1f, 1f, 1f);
Color emptyAlpha = new Color (1f, 1f, 1f, -1f);
float speed;
SpriteRenderer magicCircle;
ParticleSystem magicPar;
void Awake ()
{
magicCircle = GetComponent<SpriteRenderer> ();
magicPar = GetComponentInChildren<ParticleSystem> ();
}
void Update ()
{
float h = Input.GetAxis("Horizontal");
speed = Mathf.Abs(h);
if (speed <= 0)
{
magicCircle.color = Color.Lerp (fullAlpha, emptyAlpha, 0.5f);
magicPar.enableEmission = false;
}
else if (speed > 0)
{
magicCircle.color = fullAlpha;
magicPar.enableEmission = true;
}
}
}
Answer by troien · Dec 05, 2014 at 11:26 AM
Lerp basically is that you have 2 values, Value A and value B. In the third parameter you specify the time. This value should be a value between 0 and 1.
A timeValue of 0 means value A will be reurned. A timeValue of 1 means value B will be returned. A timeValue of 0.5 means a value halfway in between value A and B is returned.
So in your current setup, sinse fullAlpha and emptyAlpha are constant, you will always return the same value (A value between [1,1,1,1] and [1,1,1,-1]).
There are 2 ways of making this work.
1:
float smooth = 1f; // Increase this for higher speed
magicCircle.color = Color.Lerp(magicCircle.color, emptyAlpha, Time.deltaTime * smooth);
Also, see this tutorial, stating at 4:20 for the Color.
2:
private float currentValue = 0f;
void Update ()
{
float h = Input.GetAxis("Horizontal");
speed = Mathf.Abs(h);
if (speed <= 0)
{
currentValue = Mathf.Clamp01(currentValue - Time.deltaTime);
magicPar.enableEmission = false;
}
else if (speed > 0)
{
currentValue = 1f;
magicPar.enableEmission = true;
}
magicCircle.color = Color.Lerp(fullAlpha, emptyAlpha, currentValue);
}
You'll notice that the second method's speed stays constant, while the first method slows down at the end.
On a sidenote, Color values are values between 0 and 1. So setting alpha to -1 is normally somethig you don't want to do.
Thank you :D I would upvote you, but I don't have 15 rep yet, I spent hours trying to make it fade and I didn't think this site will have a fast response, you were a great help. I used the first method as it was shorter and I don't need the fading to be a constant rate.
This is my code now: using UnityEngine; using System.Collections;
public class $$anonymous$$agicWalk : $$anonymous$$onoBehaviour
{
Color fullAlpha = new Color (1f, 1f, 1f, 1f);
Color emptyAlpha = new Color (1f, 1f, 1f, 0f);
float smooth = 10f;
float speed;
SpriteRenderer magicCircle;
ParticleSystem magicPar;
void Awake ()
{
magicCircle = GetComponent<SpriteRenderer> ();
magicPar = GetComponentInChildren<ParticleSystem> ();
}
void Update ()
{
float h = Input.GetAxis("Horizontal");
speed = $$anonymous$$athf.Abs(h);
if (speed <= 0)
{
magicCircle.color = Color.Lerp (magicCircle.color, emptyAlpha, Time.deltaTime * smooth);
magicPar.enableEmission = false;
}
else if (speed > 0)
{
magicCircle.color = fullAlpha;
magicPar.enableEmission = true;
}
}
}
Answer by tanoshimi · Dec 05, 2014 at 11:12 AM
R,G,B, and A color components range from 0 to 1. So:
Color emptyAlpha = new Color (1f, 1f, 1f, -1f);
should be:
Color emptyAlpha = new Color (1f, 1f, 1f, 0);
Your second problem is a logic problem (well, depends on what you're trying to do). Your current code says, "if the speed is less than or equal to zero, pick a colour that's halfway inbetween fullAlpha and emptyAlpha" - is that really what you meant? There's no "fading" as such.
I wanted "If the speed >= 0, fade the sprite so it disappears, else, reappear"
If I set the "A" to 0 ins$$anonymous$$d of -1, the sprite only becomes transparent ins$$anonymous$$d of disappearing, I thought lerp was essentially fade so I used it, is there another way to fade?
Lerp(a,b,c) means "Pick the point that is c fraction of the distance between a and b". If you want to fade a colour, then you're going to have to adjust c (as c approaches 1 then the output becomes closer to b. As c approaches 0 then the output gets closer to a).
Currently, you have c fixed at 0.5, which is why you're always getting exactly half semi-transparent, because that's what's halfway inbetween a and b.
Thank you for your help, I changed the -1 to 0 and it worked better but I finally got the fade working using the code from troien. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613173249im_/https://answers.unity.com/themes/thub/images/avi.jpg)