Hi all,
My basic premise here is to have some text display in front of the player, and if his raycast collides, for it to fade to alpha 255, and if it is not colliding, for it to fade to alpha 0. The problem here is getting it to fade to these points from its current transparency, say midway. I cannot seem to update the text’s current alpha to be lerped from.
Here is my code:
using UnityEngine;
using System.Collections;
using TMPro;
public class TextFadeInOut : MonoBehaviour {
TextMeshPro myTMP;
Color32 myTMPColor;
public GameObject player;
Raycasting rayScript;
public Color32 tmpColorTrans;
public Color32 tmpColorOpaque;
//public Color32 tmpColorCurrent;
public float t = 0;
public float fadeTime;
// Use this for initialization
void Start () {
myTMP = GetComponent<TextMeshPro>();
myTMPColor = myTMP.color;
rayScript = player.GetComponent<Raycasting>();
print (myTMPColor);
print (myTMP.color);
tmpColorTrans = new Color32 (myTMPColor.r, myTMPColor.g, myTMPColor.b, 0);
tmpColorOpaque = new Color32 (myTMPColor.r, myTMPColor.g, myTMPColor.b, 255);
}
// Update is called once per frame
void Update () {
Color32 tmpColorCurrent = new Color32 (myTMPColor.r, myTMPColor.g, myTMPColor.b, myTMPColor.a);
print (tmpColorCurrent);
if (rayScript.rayHit == true){
if (myTMP.color.a != 255){
myTMP.color = Color32.Lerp(tmpColorCurrent, tmpColorOpaque, t);
if (t < 1){
t += Time.deltaTime/fadeTime;
}
}
}
else if (rayScript.rayHit == false){
print ("noray");
if (myTMP.color.a > 0){
myTMP.color = Color32.Lerp(tmpColorCurrent, tmpColorTrans, t);
if (t < 1){
t += Time.deltaTime/fadeTime;
}
}
}
}
}
thank you all, I admire your help