Pong-Pong/Assets/Scripts/HealthBar.cs
Gerard Gascón 16da8e4dde init
2025-04-24 17:09:22 +02:00

39 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour{
public int maxHp;
[HideInInspector] public float hp;
public Image healthBar;
public Image timer;
[HideInInspector] public float time;
GameController gameController;
// Start is called before the first frame update
void Start(){
gameController = FindObjectOfType<GameController>();
hp = maxHp;
}
// Update is called once per frame
void Update(){
if (gameController.ready){
timer.transform.localScale = new Vector2(time, timer.transform.localScale.y);
time = Mathf.Clamp(time + Time.deltaTime / 30, 0f, 1f);
}else{
timer.transform.localScale = new Vector2(0, timer.transform.localScale.y);
}
}
public void TakeDamage(float damage){
hp -= damage;
if(hp > 0 && hp > damage){
healthBar.transform.localScale = new Vector2(hp / maxHp, healthBar.transform.localScale.y);
}else if(hp < damage){
healthBar.transform.localScale = new Vector2(0, healthBar.transform.localScale.y);
}
}
}