Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by davidflynn2 · Aug 28, 2013 at 07:56 PM · c#guistay in place

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
     
     }
 
 }
     
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image davidflynn2 · Sep 03, 2013 at 09:06 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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 ) 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges