- Home /
Health bar stay in place
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
}
}
I now understand what going wrong and tried the code below it still does not stay in place. Any one know any tutorials on making an GUI stay in place or making one that stays in place. I really have to get this figured out and need help. Please some one help me.
Answer by perchik · Aug 28, 2013 at 08:19 PM
The problem is that when you draw the texture you tell it to have a fixed size
problem:
GUI.DrawTexture(new Rect(317, Screen.height -54,
(Screen.width/7.5f)*curShipHealth/shipHealthMax, 7),
foreground, ScaleMode.StretchToFill);
The short answer is to either convert all of your sizes and positions into relative numbers or switch to using GuiLayout.
To do relative sizes, you'd have to figure out what percent of the screen you want the healthbar to be and what percent you want it to start at. You could figure out these numbers relatively easy if you use the numbers you have and figure out the screen size you like the orientation at. Say your height is 600 and your width is 800, then your rect would be:
new Rect( 317/800f * Screen.width,
(600-54)/600f * Screen.height,
((Screen.width/7.5f)*curShipHealth/shipHealthMax )/800f) *Screen.width , //this line could probably be simplified
(7/600f)* Screen.height )
Your answer
![](https://koobas.hobune.stream/wayback/20220613113149im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers
Trying to Highlight text in order to copy and paste, but do not want the text to be editable. 1 Answer
Can EditorWindows dynamically display interfaces of other Editor Windows ? 1 Answer