- Home /
im having a problem lerping text color between 2 color
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CharacterStats : MonoBehaviour
{
private CharacterController characterController { get { return GetComponent<CharacterController>(); } set { characterController = value; } }
private RagdollManager ragdollManager { get { return GetComponentInChildren<RagdollManager>(); } set { ragdollManager = value; } }
public PlayerUI UI { get { return GetComponent<PlayerUI>(); } set { UI = value; } }
private CharacterStats stats;
private Weapon wp { get { return GetComponent<Weapon>(); } set { wp = value; } }
private GameController gc{get{return GetComponent<GameController>();} set {gc = value;}}
[Range(0, 100)]
public int CurrentHealth = 100;
public int Highheal = 100;
public int Midheal = 50;
public int Lowheal = 25;
public int MaxHealth = 100;
public int faction;
public MonoBehaviour[] scriptsToDisable;
public Image damageImage;
public Image healImage;
public float healFlashSpeed = 10f;
public Color healFlashColor = new Color(1f, 0f, 0f, 1f);
public float flashSpeed = 5f;
public Color flashColor = new Color(1f, 0f, 0f, 0.5f);
bool damage;
bool heal;
public Color LowHealth = Color.red;
public Color FullHealth = Color.green;
void Awake()
{
CurrentHealth = MaxHealth;
CurrentHealth = Mathf.Clamp(CurrentHealth, 0, 100);
}
void Start()
{
}
void Update()
{
if (gameObject.tag == "Player")
{
if (heal)
{
healImage.color = healFlashColor;
}
else
{
healImage.color = Color.Lerp(healImage.color, Color.clear, healFlashSpeed * Time.deltaTime);
}
heal = false;
}
if (gameObject.tag == "Player")
{
if (damage)
{
damageImage.color = flashColor;
}
else
{
damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damage = false;
}
healthColorChange();
}
public void Damage(int amount)
{
damage = true;
CurrentHealth -= amount;
if (CurrentHealth <= 0)
{
Die();
}
}
public void Die()
{
characterController.enabled = false;
if (scriptsToDisable.Length == 0)
{
return;
}
foreach (MonoBehaviour script in scriptsToDisable)
script.enabled = false;
if (ragdollManager != null)
ragdollManager.RagDoll();
if(gameObject.CompareTag("Enemy"))
{
Destroy(gameObject, Random.Range(4f, 5f));
}
if(gameObject.tag == "Player")
{
if(CurrentHealth <=0)
{
gc.LastCheckPointPos = transform.position;
}
}
}
public void HighHeal()
{
if(gameObject.tag == "Player")
{
if (CurrentHealth > 100)
return;
if(CurrentHealth < 100)
CurrentHealth = Highheal;
heal = true;
}
}
public void MidHeal()
{
if(gameObject.tag == "Player")
{
CurrentHealth += Midheal;
heal = true;
}
}
public void LowHeal()
{
if(gameObject.tag == "Player")
{
CurrentHealth += Lowheal;
heal = true;
}
}
public void healthColorChange()
{
if(gameObject.tag == "Player")
{
UI.HealthText.color = Color.Lerp(FullHealth, LowHealth, CurrentHealth);
}
}
}
then this is my player UI
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class PlayerUI : MonoBehaviour { public Text ammoText; public Text HealthText; }
Answer by highpockets · Mar 07, 2019 at 02:19 PM
Looking at your code, I don't see you updating healFlashSpeed anywhere. So it looks like you are giving the lerp that takes that time value pretty much the same value every frame. You should change it each frame like so:
healFlashSpeed = healFlashSpeed + Time.deltaTime;
Plus the t value of color.lerp is clamped to 0-1.. If you pass a value equal to or more than 1, you will always get the end result (second color passed). To have the interpolation work properly, you need to treat 1 as if it is 100 percent, so 0.25f is 25 percent, 0.5f is 50 percent, etc. But I see that you are starting with a healFlashSpeed of 20f which is going to show 100 percent because its over 1.0f
im sorry that you didn`t get my problem correctly because of my poor language skills, but my problem is my HealthText is not changing color from fullhealth to lowhealth which is green and red.
Ok, that was my issue as I’m looking now and I must have missed “text” in your question. but it looks like the same issue as I explain. The first parameter will be returned from the lerp function if you pass the function 0 as the third float parameter, meaning full health should be 0 as the 3rd parameter and low health should be 1 as the third param. If you pass it 100, it should return your second parameter because it is more than 1
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
NullRefenceException error 1 Answer
NullReferenceException for reasons I don't understand. 1 Answer
NullReference when accessing GameObject in array (C#) 1 Answer