Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

get a HP bar to follow a charakter

Discussion in 'Scripting' started by simmi91, Feb 12, 2013.

  1. simmi91

    simmi91

    Joined:
    Jan 10, 2013
    Posts:
    4
    hi, I'm trying to have a HP bar over each enemy in my game. As it is now I do have a bar that follows the enemy,but only in local coordinates. Converting it to world coordinated didn't work...

    Could I please have some help with this, or are there any other (better) ways to do this?

    Here's part of the code:
    Code (csharp):
    1.  
    2. public class EnemyHealth : MonoBehaviour {
    3.  
    4. void Update ()
    5.     {
    6.         AddjustCurrentHealth(0);
    7.                
    8.         pLeft = transform.position.x;  
    9.         pTop  = transform.position.z;
    10.        
    11.         //*I believe this is how to convert the positions into World space, but it doesn't work.*//
    12.         //pLeft = transform.Translate(transform.position.x * Time.deltaTime, Space.World);
    13.         //pTop  = transform.Translate(transform.position.z * Time.deltaTime, Space.World);
    14.        
    15.     }
    16.        
    17.     void OnGUI()
    18.     {
    19.         GUI.skin.box = style;
    20.         GUI.Box(new Rect(pLeft, pTop, healthBarLength, pHeight), curHealth + "/" + maxHealth);
    21.     }
    22.  
    23. }
    24.  
     
  2. PaulR

    PaulR

    Joined:
    Nov 14, 2012
    Posts:
    43
  3. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    You may need to use this line at some point.

    Code (csharp):
    1.  
    2.   Camera.main.WorldToScreenPoint(Vector3(0,0,0))
    3.  
    I for the longest time have been looking for a working solution, and have only come close. As far as I know there are no good tutorials or scripts for having your gui follow the NPC'S. :(

    Good Luck.
     
  4. bugzilla

    bugzilla

    Joined:
    Dec 9, 2008
    Posts:
    196
  5. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Wish I could see it lol. It's mega blurry on my screen. I do have a huge HD monitor but it doesn't help. What video software do you use? I would love to see what code you used to get the gui to follow the npc's correctly. One problem I'm having with my code is that the Gui gets bigger as the npc gets further away.
     
  6. simmi91

    simmi91

    Joined:
    Jan 10, 2013
    Posts:
    4
    Thank you for the help! Very much appreciated :)

    I looked at your tut, bugzilla, and it was very helpful. I'd like to use C# instead so I converted it, but since I've never used Instantiate before I do not know to use that line in C#.... any tips?

    Another question; how would you script the damage taken by the enemy with bugzillas hpbar? (Edit the hpbars length with different damage rates)
     
  7. mark-velthuis

    mark-velthuis

    Joined:
    Feb 10, 2011
    Posts:
    47
    I would personaly advice to use 3D planes with textures instead of GUI components. No coordinate conversion is neded. And it could be much more efficient aswell since Unity's GUI system isn't too great.