Player Health Glitch?
I have built a player health system with a health bar, and everything seems to be working fine, the only problem is that the system will not work with a 4 health maximum. Here is the script:
public class PlayerHealth : MonoBehaviour {
public GameObject blood;
public float Health;
public float MaxHealth;
public Image HealthBarFill;
void Update () {
HealthBarFill.fillAmount = Health / MaxHealth;
if(Health <= 0)
{
Destroy(gameObject);
}
if(Health > MaxHealth)
{
Health = MaxHealth;
}
}
void OnCollisionEnter (Collision col) {
if(col.gameObject.tag == "EnemyBullet")
{
Instantiate(blood, this.transform.position + this.transform.forward/2, this.transform.rotation);
Health -= 1;
}
}
}
Now, if I set the player health/max health in the inspector to 5 or higher, it works just fine. Five hits to die and the bar reflects 5 health. If I do 8, it also works fine. If I put it on 4 or lower, I only get two hits. The bar works fine. It reflects the two hits, but if I set it to 4, it's just two health. 3 is just two hits to die. 2 works fine, only two health. At first, I thought it was just registering multiple hits on the collision, but that does not appear to be the case. It counts the health 100% accurately on anything above four, but 4 is the actual amount I wanted to use, and now I just want to know what is wrong. Any ideas?
Your answer
Follow this Question
Related Questions
"Clipping", "Jumping" UI slider, attached to the 2d game object ( health bar) 0 Answers
"Clipping", "Jumping" UI slider, attached to the 2d game object ( health bar) 0 Answers
Custom Slider Shapes causing fill to break. 0 Answers
How to make GUI variables compatible with the UI system? 0 Answers
How to place sprite above GameObject? 2 Answers