SpriteHealthBar.cs 781 B

1234567891011121314151617181920212223242526272829
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SpriteHealthBar : MonoBehaviour
  5. {
  6. private Vector3 startScale;
  7. public Transform healthBarFill;
  8. public float health;
  9. void Awake()
  10. {
  11. startScale = healthBarFill.localScale;
  12. }
  13. public void SetHealth(float amount, float maxHealth = 100f){
  14. if(maxHealth == 0 ){Debug.Log("max health is 0"); return;}
  15. health = amount;
  16. float amountMult = amount / maxHealth;
  17. if(amountMult<0){amountMult=0;}
  18. //Debug.Log($"Setting health on {transform.name}: {amount} / {maxHealth}");
  19. healthBarFill.localScale = new Vector3(startScale.x * amountMult, startScale.y,startScale.z );
  20. }
  21. }