Search Unity

Healthbar Questions

Discussion in 'Immediate Mode GUI (IMGUI)' started by kekejones, Jul 23, 2014.

  1. kekejones

    kekejones

    Joined:
    Jul 17, 2014
    Posts:
    3
    Hello everyone, I am a unity noob who is having a hard time making a hud for a class project. I found 2 scripts on a website, 1 that holds the healthbar info and the other one is just to test the healthbar and show what it'd look like if it took damage.

    The problem I am having with the scripts is that whenever I test the damage, the health part like scrolls to the left while decreasing right to left. I would like the bar to remain static while it gets erased on the right side with each hit like a normal healthbar. Can anyone help me out?

    Healthbar script
    Code (JavaScript):
    1. #pragma strict
    2. // JavaScript
    3. var backgroundTexture : Texture;
    4. var foregroundTexture : Texture;
    5. var frameTexture : Texture;
    6. var healthWidth: int = 199;
    7. var healthHeight: int = 30;
    8. var healthMarginLeft: int = 41;
    9. var healthMarginTop: int = 38;
    10. var frameWidth : int = 266;
    11. var frameHeight: int = 65;
    12. var frameMarginLeft : int = 10;
    13. var frameMarginTop: int = 10;
    14. function OnGUI () {
    15.     GUI.DrawTexture( Rect(frameMarginLeft,frameMarginTop, frameMarginLeft + frameWidth, frameMarginTop + frameHeight), backgroundTexture, ScaleMode.ScaleToFit, true, 0 );
    16.     GUI.DrawTexture( Rect(healthMarginLeft,healthMarginTop,healthWidth + healthMarginLeft, healthHeight), foregroundTexture, ScaleMode.ScaleAndCrop, true, 0 );
    17.     GUI.DrawTexture( Rect(frameMarginLeft,frameMarginTop, frameMarginLeft + frameWidth,frameMarginTop + frameHeight), frameTexture, ScaleMode.ScaleToFit, true, 0 );
    18. }
    Damage preview script
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var guiHealth : GameObject;
    4. private var healthBarScript: HealthBarScript;
    5.  
    6. /*
    7. Start is called only once in the lifetime of
    8. the behavior. So, it's a good place to initialize
    9. things only once.
    10. */
    11. function Start() {
    12.    
    13.     guiHealth = GameObject.Find("GUI Health");
    14.     healthBarScript = guiHealth.GetComponent("HealthBarScript");
    15.    
    16.     // Set initial value of the health...
    17.    
    18.     // Uncomment the line below and call reduceHealth() in the Update() method to watch health decrease
    19.     healthBarScript.healthWidth = 199;
    20.    
    21.     // Uncomment the line below and call increaseHealth() in the Update() method to watch health increase
    22.     // healthBarScript.healthWidth = -8;
    23.    
    24. }
    25.  
    26.  
    27. function Update() {
    28.    
    29.        reduceHealth();
    30.      
    31.        //increaseHealth();
    32.      
    33. }
    34.  
    35. /*
    36. Only decrease the health bar if it's greater than the min width it should ever be;
    37. because we do not want it decreased beyond the left of its frame.
    38. */
    39. function reduceHealth() {
    40.    if(healthBarScript.healthWidth > -8) {
    41.        healthBarScript.healthWidth = healthBarScript.healthWidth - 1;
    42.    }  
    43. }
    44.  
    45. /*
    46. Only increase the health bar if it's less than the max width it should ever be;
    47. because we do not want it stretched out beyond its frame.
    48. */
    49. function increaseHealth() {
    50.    if(healthBarScript.healthWidth < 199) {
    51.        healthBarScript.healthWidth = healthBarScript.healthWidth + 1;
    52.    }
    53. }
     
  2. WiNeTel

    WiNeTel

    Joined:
    Jul 10, 2013
    Posts:
    4
  3. BlackPanda

    BlackPanda

    Joined:
    Jan 24, 2014
    Posts:
    78
    I guess that's because of the way Rect() works. I have had same sort of problem some time before. What I did was I made the health bar a Texture2D gameobject and scaled its transform up and down whenever health changed. Hope this helps. :)