firala
firala

Reputation: 63

Lerp Text alpha based on distance between 2 objects

for a project I need to modify the alpha value of a UI Text in Unity depending on the distance the player / viewer is from it. If he is closer than "visibleDistance", it should be completely visible / have an alpha value of 1.

The distance gets passed from OnTriggerStay(Collider other) in the viewer's behaviour to this function on the Text's Game Object:

public void UpdateTransparency(float maxDistance, float distance){
    maxDistance -= visibleDistance;
    float tmp = maxDistance/distance;
    tmp = 1/tmp;
    text.color = Color.Lerp(text.color, Color.clear, tmp);
}

If I print out tmp, it gives "correct looking" values, but nothing is happening to the text's color (text is correctly assigned in the Start function). The text is rendered on a World Space canvas.

Would be great if someone could help me with that :) Thanks in advance!


Update: Fixed it with a modified solution below:

public void UpdateTransparency (Vector3 viewerPos, float maxDistance){
    float distanceApart = Vector3.Distance(viewerPos, this.transform.position));
    float lerp = mapValue(distanceApart, maxDistance-3f, maxDistance, 0f, 1f);
    Color lerpColor = text.color;
    lerpColor.a = Mathf.Lerp(1, 0, lerp);
    text.color = lerpColor;
}


float mapValue(float mainValue, float inValueMin, float inValueMax, float outValueMin, float outValueMax)
{
    return (mainValue - inValueMin) * (outValueMax - outValueMin) / (inValueMax - inValueMin) + outValueMin;
}

Thanks everyone.

Upvotes: 1

Views: 370

Answers (1)

Programmer
Programmer

Reputation: 125315

If you want to lerp the alpha of a GameObject, lerp the alpha, not the Color. This means should use use Mathf.Lerp instead of Color.Lerp. The Mathf.Lerp value should be assigned to text.color.a property and you should pass 0 and 1 to it.

Assusming that your tmp variable is good and returns values between 0 and 1, the code below:

float tmp = maxDistance/distance;
tmp = 1/tmp;
text.color = Color.Lerp(text.color, Color.clear, tmp);

should be replaced with something like this:

float tmp = maxDistance/distance;
tmp = 1/tmp;
Color lerpColor = text.color;
lerpColor.a = Mathf.Lerp(1, 0, tmp);
text.color = lerpColor;

If there are still issues with your code then there is a similar question for this here but it is for a MeshRenderer. I modified that code to work with Text component. See below.

public GameObject obj1;
public GameObject obj2;

const float MAX_DISTANCE = 200;

Text text;

void Start()
{
    text = GameObject.Find("Text").GetComponent<Text>();
}


void Update()
{
    //Get distance between those two Objects
    float distanceApart = getSqrDistance(obj1.transform.position, obj2.transform.position);
    UnityEngine.Debug.Log(getSqrDistance(obj1.transform.position, obj2.transform.position));

    //Convert 0 and 200 distance range to 0f and 1f range
    float lerp = mapValue(distanceApart, 0, MAX_DISTANCE, 0f, 1f);

    //Get old color
    Color lerpColor = text.color;

    //Lerp Alpha between 1 and 0
    lerpColor.a = Mathf.Lerp(1, 0, lerp);

    //Apply modified alpha
    text.color = lerpColor;
}

public float getSqrDistance(Vector3 v1, Vector3 v2)
{
    return (v1 - v2).sqrMagnitude;
}

float mapValue(float mainValue, float inValueMin, float inValueMax, float outValueMin, float outValueMax)
{
    return (mainValue - inValueMin) * (outValueMax - outValueMin) / (inValueMax - inValueMin) + outValueMin;
}

Upvotes: 1

Related Questions