I have the following health system the problem is that if the screen resultion changes the green health bar leaves the gui it should show in. How do I make it so that the GUI texture for the health bar stays in place with different screen resolutions.
using UnityEngine;
using System.Collections;
public class ShipHealth : MonoBehaviour {
public int shipHealthMax = 100;// The ships Max health
public int curShipHealth = 100; // The ships current health
public GameObject shield; //The shield you are using
public GameObject ship; // The ship itself
public GameObject remains; // The remains
public GameObject smoke; // The smoke
public Transform player; // Ships transform
public Transform location1; // The teleport after death location
Rect box = new Rect(10, 50, 160, 17);// Starte Cordonates
public Texture2D background; // Texture for the background of your health bar
public Texture2D foreground; // Texture for the forground of your health bar
public Texture2D Hud; // The hud of your health bar
public Texture2D SpaceMan; // The spaceman shown in the hud
void Start()
{
StartCoroutine(addHealth()); // Starts the health recovery system
}
void Update ()
{
AddjustCurrentHealth(0); //keeps the current health uptodate
if(curShipHealth == 0)//checks if the curship health is 0
{
StartCoroutine(dead());//Starts the death process.
}
if(curShipHealth <50)//Checks if maxHealth is set to less then 1.
{
smoke.SetActive (true);
}
else
{
smoke.SetActive (false);
}
}
public void AddjustCurrentHealth(int adj) //adjusts the current health
{
curShipHealth += adj;//This is to recieve heals or dammage to the CurHealth. The number is passed in then assigned to curHealth.
if(curShipHealth < 0)//Checks if the players health has gone below 0.
{
curShipHealth = 0;// If players health has gone below 0 set it to 0.
}
if(curShipHealth> shipHealthMax)//Checks if player health is higher then maxHealth.
curShipHealth = shipHealthMax;//If players health is higher then maxHealth set it = to maxHeatlh
if(shipHealthMax <1)//Checks if maxHealth is set to less then 1.
shipHealthMax = 1;//If maxHealth is set below 1, this sets it to 1.
}
void OnCollisionEnter(Collision collision) //When collided
{
if(!shield.activeSelf)// Checks to make sure the shield is not active
{
DamageInflictor di = collision.gameObject.GetComponent<DamageInflictor>();
if(di)
{
print ("You Took Damage");
curShipHealth -= di.damage;
}
}
}
void OnGUI()
{
GUI.DrawTexture(new Rect (0,Screen.height - 155,Screen.width,130),Hud); //Draws the Hud
GUI.DrawTexture(new Rect (20,12,121,141),SpaceMan); // Draws the Spaceman
// GUI.DrawTexture(new Rect(148, 38, box.width, box.height), background, ScaleMode.StretchToFill); //Draws the background health bar
GUI.DrawTexture(new Rect(317, Screen.height -54, (Screen.width/7.5f)*curShipHealth/shipHealthMax, 7), foreground, ScaleMode.StretchToFill); // Shows the current health
GUI.Label(new Rect(180,37,100,40),"Ship HP "+curShipHealth.ToString());//Displays the shields health in text.
GUI.contentColor = Color.green;//Sets the text color red
}
IEnumerator addHealth ()//Add health to the ship shield
{
while (true)
{
if (curShipHealth < 100)
{ // if health < 100...
curShipHealth += 1; // increase health and wait the specified time
yield return new WaitForSeconds(5);
}
else
{ // if health >= 100, just yield
yield return null;
}
}
}
IEnumerator dead ()// The dead function
{
GameObject varGameObject = GameObject.Find("Star Blaster"); // Finds the main ship
varGameObject.GetComponent<ShipThrusterControle>().enabled = false; // Finds the fly script and turns it off if we die
ship.SetActive(false); // sets the ship invisible
remains.SetActive (true); ///Sets the reamins visible
yield return new WaitForSeconds(3); //waits 3 seconds
player.transform.position = location1.transform.position; //changes our position and gets us our of danger
varGameObject.GetComponent<ShipThrusterControle>().enabled = true;// turns flight script back on
curShipHealth = shipHealthMax; // Sets our health back to max
ship.SetActive (true); //sets ship seable
remains.SetActive(false); // turns remains off
}
}