So this is my code:
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public float health;
public float maxHealth;
public Image healthBar;
public Transform player;
public Animation healthFadeAnimation;
void Start()
{
health = maxHealth;
}
void Update()
{
healthBar.fillAmount = Mathf.Clamp(health/maxHealth, 0, 1);
if(health < 0)
{
health = 0;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Hazard") || collision.gameObject.CompareTag("Enemy"))
{
healthFadeAnimation.Play();
}
}
}
You may be able to tell that I want to make it so that everytime player takes damage the healthbar appears and then fades. I have an animation asset named HealthFade and I’m trying to drop it on the Health Fade Animation but it won’t let me do it. Am I making a wrong approach here?