Search Unity

Enemy Health Bar above a Game object Help This will help a lot of people if answered!

Discussion in 'Scripting' started by NikoBusiness, Jul 2, 2013.

  1. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    hey guys i want to add health bar Gui / texture or whatever it can be, above the a gameobject and follow it and it has to be billboard i think, smething like this : example video : http://www.youtube.com/watch?v=EkT_-3m9NPE
    please help me! any tutorial or any little help to start,to understand how to script it
    ( i know how to make a script like a healthbar to change scale with a variable i just need the help to make a Gui or a texture to follow the Transform of the gameobject)
     
    Last edited: Jul 3, 2013
  2. pickle-chips

    pickle-chips

    Joined:
    Aug 12, 2012
    Posts:
    44
    Lol haha hey Niko, I'm happy to see my video in your thread x)

    You need to use Camera.WorldToScreenPoint (http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html)

    this takes a world point and puts it on the screen.You would attach this script to the object you want to display the healthbar. the code should look similar to this (c#):

    Code (csharp):
    1.  
    2. Vector3 pos;
    3.  
    4. pos = Camera.main.WorldToScreenPoint(new Vector3(transform.position.x + xOffset, transform.position.y + yOffset, transform.position.z); //The offsets are to position the health bar so it's placed above and centered to the character
    5. pos.y = Screen.height - pos.y;
    6.  
    7.  
    From there you just use GUI.DrawTexture and draw the healthbar, using "pos.x" and "pos.y" as the coordinates in the Rect.

    If you still have trouble after this I will try and explain some more. (I might just post my code)
     
  3. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GuiHealth : MonoBehaviour {
    5.     public Vector3 pos;
    6.     public float xOffset;
    7.     public float yOffset;
    8.     public Texture aTexture;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         pos = Camera.main.WorldToScreenPoint(new Vector3(transform.position.x + xOffset, transform.position.y + yOffset,transform.position.z));
    18.         pos.y = Screen.height - pos.y;
    19.          
    20.    
    21.     }
    22.     void OnGUI(){
    23.     GUI.DrawTexture(new Rect(pos.x,pos.y,100,40),aTexture, ScaleMode.ScaleToFit, true, 10.0F);
    24.     }
    25. }
    it works but not fine ... its changing position... and i think i know why ... because my camera is moving too... so i have to add some more scripting :/
     
    Last edited: Jul 3, 2013
  4. XGundam05

    XGundam05

    Joined:
    Mar 29, 2012
    Posts:
    473
    What are xOffset and yOffset for? If it's for the offset for the GUI texture on the screen, then you should probably be adding the xOffset to the pos variable instead.

    The yOffset should be fine where it's at, because you want the bar relative to the top of the character, so if the camera rotates to a steeper angle, the bar is still relative to the top of the character and not floating off somewhere else. But the xOffset sounds like you're using it for the screen space, and if it's added to the world space instead you'll then get it moving off center of the character.
     
  5. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    @XGundam05 yeah man when i dont change the value of the xOffset its working fine...
    so im not gonna use the xOffset...
     
  6. XGundam05

    XGundam05

    Joined:
    Mar 29, 2012
    Posts:
    473
    You can use it, just use it on your pos variable. So, from what I'm assuming, something like:

    Code (csharp):
    1.  
    2. pos.x += xOffset;
    3.  
    And with your health bar being 100 px wide, you'd set xOffset to -50.0 to center the image.
     
  7. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    hey guys i have another problem with the gui.drawtexture
    i have 2 gui textures in the exactly same coordinates for the healthbar
    and i want 1 of them to be in the front to make the healthbar illusion with the changing sclae;
    how can i select wich one is going to be in the front?
    another problem i have is the 2nd gui that is scaling to showup healthbar illusion
    if i change the rect width value its scaling the whole guitexture :/

    these are my two scripts




    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GuiHealth2 : MonoBehaviour {
    5.     public Texture bTexture;
    6.     private GuiHealth script;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         script = GetComponent<GuiHealth>();
    16.    
    17.     }
    18.     void OnGUI(){
    19.         if(script.scelMotor.Distance < 10)
    20.     GUI.DrawTexture(new Rect(script.pos.x,script.pos.y,70 * script.tisekato ,50),bTexture, ScaleMode.ScaleToFit, true, 10.0F);
    21.     }
    22. }
    23.  
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GuiHealth : MonoBehaviour {
    5.     public Vector3 pos;
    6.     public float xOffset;
    7.     public float yOffset;
    8.     public Texture aTexture;
    9.     public SceletonMotor scelMotor;
    10.     public SkeletonHealth guiHscript;
    11.     public float tisekato;
    12.    
    13.    
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.         guiHscript = GetComponent<SkeletonHealth>();
    24.         scelMotor = GetComponent<SceletonMotor>();
    25.         pos = Camera.main.WorldToScreenPoint(new Vector3(transform.position.x + xOffset, transform.position.y + yOffset,transform.position.z));
    26.         pos.y = Screen.height - pos.y;
    27.         tisekato = HealthValue(guiHscript.maxHealth,guiHscript.health);
    28.         print(tisekato);
    29.        
    30.        
    31.        
    32.          
    33.    
    34.     }
    35.     void OnGUI(){
    36.         if(scelMotor.Distance < 10)
    37.     GUI.DrawTexture(new Rect(pos.x,pos.y,69,50),aTexture, ScaleMode.ScaleToFit, true, 10.0F);
    38.     }
    39.     public float HealthValue(float max,float current){
    40.         float sum = current * 100;
    41.         float sum1 = sum / max;
    42.        
    43.         return(sum1 / 100);
    44.     }
    45.    
    46. }
    47.  
     
    Last edited: Jul 3, 2013
  8. pickle-chips

    pickle-chips

    Joined:
    Aug 12, 2012
    Posts:
    44
    Yeah that's actually what I did in my code, but I figured it wouldn't make a differenct to just put it in the vector 3., so for simplicity to this thread that's what I said. I guess I was wrong! :s

    And also nikko, to decide which one is in front you just call it's draw function last. I'm not quite sure what you mean about your second problem.
     
    Last edited: Jul 4, 2013
  9. epicjosh

    epicjosh

    Joined:
    Oct 3, 2014
    Posts:
    3
    can you convert it to javascript?
     
  10. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Frankly, this is a waste of effort, since in 4.6 the new GUI stuff actually makes floating health bar type stuff ridiculously easy.
     
    Miscellaneous likes this.
  11. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Miscellaneous likes this.