- Home /
Question by
Dracolyte · Nov 24, 2020 at 05:57 PM ·
alphaspriterenderer
Decreasing Alpha from object with Hp value
What I cant get to work is, that the alpha value of the SpriteRenderer decreases with the Hp of the player. The hp of the player decreases over time and I multiply the value by 2,55 and apply the value to the alpha of the SpriteRenderer. The problem is, that the alpha doesn't change in the game. But the alpha is on 0 if the Hp of the player is 0 or below. How do I fix that?
public float playerHp = 100f;
public float HpDecreasePerSec = 10f;
public float playerDmg = 1f;
public float score = 0;
public float money = 0f;
public Color alpha;
private SpriteRenderer _spriteRenderer;
private void Start()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
alpha = GetComponent<SpriteRenderer>().color;
}
private void Update()
{
alpha.a = 2.55f * playerHp;
_spriteRenderer.color = alpha;
playerHp -= HpDecreasePerSec * Time.deltaTime;
Debug.Log(playerHp);
}
}
Comment
Best Answer
Answer by FeedMyKids1 · Nov 24, 2020 at 06:10 PM
Hi. Alpha is actually a 0-1 value.
I see what you're trying to do is 255 when the player is at full health, so it's the same concept:
color.a = playerHp/100f;