Heya guys.
This script works fine with the health and all that its just that the HealthBar GUI doesnt quite work as it should. My knowledge of scripting is poor I´m afraid and I’m at a complete loss as to what is wrong. It’s propably just one line of code or so that needs to be added, changed or subtracted somewhere.
I’ve got 5 HP, that part works great, but the GUI doesnt show any “bar” or signs of life whatsoever until I have 1 HP left, then it leaps into action and displays a full HP bar and when I get hit and loose the last life it naturally goes blank again, which it should. But why doesnt it show anything until I only have 1 HP left? And why a full HP Bar at 1 HP? (The script is 2 scripts put together but the problem is the same even when they were 2 seperate scripts) Oh and I’m sorry the script looks like a butchered frankensteins monster.
Heres the script.
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D), typeof(Animator))]
/// <summary>
/// Handle hitpoints and damages
/// </summary>
public class HealthScript : MonoBehaviour
{
// Animator Component
private Animator animator;
void Start ()
{
// Get the Animator component
animator = GetComponent<Animator> ();
}
public Texture2D emptyTex;
public Texture2D fullTex;
public Vector2 pos = new Vector2(20, 40);
public Vector2 size = new Vector2(20, 60);
public GUIStyle progress_empty, progress_full;
public float minhp = 0;
public float maxhp = 5;
float barDisplay = 0;
/// <summary>
/// Total hitpoints
/// </summary>
public int hp = 5;
/// <summary>
/// Enemy or player?
/// </summary>
public bool isEnemy = true;
void OnGUI() {
// draw the background:
GUI.BeginGroup(new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box(new Rect(0,0, size.x, size.y), emptyTex, progress_empty);
// draw the filled-in part:
GUI.BeginGroup(new Rect (0, (size.y - (size.y * barDisplay)), size.x, size.y * barDisplay));
GUI.Box(new Rect(0,0, size.x, size.y), fullTex, progress_full);
GUI.EndGroup();
GUI.EndGroup ();
}
void Update() {
//for this example, the bar display is linked to the current time,
//however you would set this value based on your desired display
//eg, the loading progress, the player's health, or whatever.
barDisplay = hp;
// barDisplay = MyControlScript.staticHealth;
}
/// <summary>
/// Inflicts damage and check if the object should be destroyed
/// </summary>
/// <param name="damageCount"></param>
public void Damage(int damageCount)
{
hp -= damageCount;
if (hp <= 0)
{
SoundEffectsHelper.Instance.MakeExplosionSound();
SpecialEffectsHelper.Instance.Explosion(transform.position);
// Dead!
Destroy(gameObject);
}else{
animator.SetTrigger("Damage");
}
}
void OnTriggerEnter2D(Collider2D otherCollider)
{
// Is this a shot?
PlayerShotDamage shot = otherCollider.gameObject.GetComponent<PlayerShotDamage>();
if (shot != null)
{
// Avoid friendly fire
if (shot.isEnemyShot != isEnemy)
{
Damage(shot.damage);
// Destroy the shot
Destroy(shot.gameObject); // Remember to always target the game object, otherwise you will just remove the script
}
}
}
}