Question by
dfng11 · May 13 at 05:06 PM ·
2d game2d-platformer
Image radial 360 Health Bar doesnt decrease
I have searched, and the others people similar situation doesn't helped me.
I have an Image UI radial 360 and it doesn't decrease when player loses health.
Here is the code:
public class HealthBar : MonoBehaviour
{
[SerializeField] private float maxHealth = 100f;
[SerializeField] private Image healthBar;
private float health = 0f;
void Start()
{
health = maxHealth;
healthBar.fillAmount = maxHealth;
}
public void UpdateHealth(float mod)
{
health += mod;
if (health > maxHealth)
{
health = maxHealth;
}
else if (health <= 0f)
{
health = 0f;
//Destroy(gameObject);
healthBar.fillAmount = health;
}
}
void OnGUI()
{
float t = Time.deltaTime / 1f;
healthBar.fillAmount = Mathf.Lerp(healthBar.fillAmount, health, t);
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("Enemy"))
{
health = health - 5f;
healthBar.fillAmount = health;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Player is bouncing when colliding with wall object 0 Answers
Tilemap doesn't showing on window?? unity 5.6.1f1 ,Tilemap not showing on the Window or as 2D Object 0 Answers
2D movement with gravity shifting 0 Answers
hi i want to add a slid and wall jump to my script with animation, i am lost ^^ 0 Answers
How do i flip my character in 2D platformers,Im at a loss on flipping my 2D character 0 Answers