this is something im looking to do rather than a healthier, which i have.
so instead of the character just dying when the health bar reaches a point, he fades out. now i have something for the health bar, works perfectly, by all means use it.
but how can i use this to affect the players alpha?
here is what i have just now. i have tried playing with it, but i run into errors. have not seen anybody else ask this. so thought i may as well be the first
public float health = 100f;
public float healthTimer;
private SpriteRenderer healthBar;
private Vector3 healthScale;
void Start () {
healthBar = GameObject.Find ("HealthBar").GetComponent<SpriteRenderer> ();
healthScale = healthBar.transform.localScale;
}
void FixedUpdate () {
if(health <= 0){
Destroy(this.gameObject);
}
if(health > 100){
health = 100;
}
HealthUpdate();
}
void HealthUpdate(){
healthBar.transform.localScale = new Vector3(healthScale.x * health * 0.01f, 1, 1);
}
void OnCollisionEnter2D(Collision2D enemyHit){
if (enemyHit.gameObject.tag == "Enemy"){
health -= 3;
}
if(enemyHit.gameObject.tag == "HealthPickUp"){
health += 10;
}
}