This is the health bar im using as a placeholder for my script for health, I plan to make my own when i have fully finalized functionality, however currently this is used while functionality is being tweaked to work correctly as intended.
Going through the tutorial by Naman Jain to help set up health script with the health bar changing values ETC to adjust to the current health of the enemy.
Script Below
Going through the tutorial by Naman Jain to help set up health script with the health bar changing values ETC to adjust to the current health of the enemy.
Script Below
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public Fighter player;
public Texture2D frame;
public Rect framePosition;
public float width;
public float height;
public float horizontalDistance;
public float verticalDistance;
public Texture2D healthBar;
public Rect healthBarPosition;
public mob target;
public float healthPercentage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(player.opponent!=null)
{
target = player.opponent.GetComponent<mob> ();
healthPercentage = (float)target.health / target.maxHealth;
}
else{
target = null;
healthPercentage = 0;
}
}
void OnGUI()
{
if(target !=null&&player.countDown>0)
{
drawFrame();
drawBar();
}
}
void drawFrame()
{
framePosition.x = (Screen.width - framePosition.width) / 2;
GUI.DrawTexture (framePosition, frame);
}
void drawBar()
{
healthBarPosition.x = framePosition.x + framePosition.width * horizontalDistance;
healthBarPosition.y = framePosition.y + framePosition.height * verticalDistance;
healthBarPosition.width = framePosition.width * width*height;
healthBarPosition.height = framePosition.height;
GUI.DrawTexture (healthBarPosition, healthBar);
}
}
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public Fighter player;
public Texture2D frame;
public Rect framePosition;
public float width;
public float height;
public float horizontalDistance;
public float verticalDistance;
public Texture2D healthBar;
public Rect healthBarPosition;
public mob target;
public float healthPercentage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(player.opponent!=null)
{
target = player.opponent.GetComponent<mob> ();
healthPercentage = (float)target.health / target.maxHealth;
}
else{
target = null;
healthPercentage = 0;
}
}
void OnGUI()
{
if(target !=null&&player.countDown>0)
{
drawFrame();
drawBar();
}
}
void drawFrame()
{
framePosition.x = (Screen.width - framePosition.width) / 2;
GUI.DrawTexture (framePosition, frame);
}
void drawBar()
{
healthBarPosition.x = framePosition.x + framePosition.width * horizontalDistance;
healthBarPosition.y = framePosition.y + framePosition.height * verticalDistance;
healthBarPosition.width = framePosition.width * width*height;
healthBarPosition.height = framePosition.height;
GUI.DrawTexture (healthBarPosition, healthBar);
}
}