Search Unity

Health bar over enemy ?

Discussion in 'Scripting' started by skaterboy, Jul 5, 2009.

  1. skaterboy

    skaterboy

    Joined:
    Jun 6, 2009
    Posts:
    25
    Hi, I'm making a 3rd person game and want to know how to make the life of my enemies showing over there heads. Like a floating health bar over all the enemies
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    First create a guiTexture. Then to make it track a gameobject, put the following script onto it.

    Code (csharp):
    1. // This script will make a GUITexture follow a transform.
    2. var target : Transform;
    3.  
    4. function Update ()
    5. {
    6.      var wantedPos = Camera.main.WorldToScreenPoint (target.position);
    7.      transform.position = wantedPos;
    8. }
     
  3. skaterboy

    skaterboy

    Joined:
    Jun 6, 2009
    Posts:
    25
    Thanks, I've tested it but it isn't visible when I start the game.

    It's just getting away ......

    Do I need any other codes ?
     
  4. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    No, Daniels code is fine. I guess you have not set the "target" variable in the inspector to the object where the health bar should appear.
     
  5. skaterboy

    skaterboy

    Joined:
    Jun 6, 2009
    Posts:
    25
    The things that I have done is to,
    make a GUItexture then make a new script, put in his code, put the script on the texture, drag the enemy into the target variable, start the game and nothing happend .......
     
  6. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    I had a similar problem trying to get the FPS script to work (it uses a GUITexture). I never was able to get it to display.
     
  7. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Oops, I gave you bad code. Try this one instead:

    Code (csharp):
    1. var target : Transform;
    2.  
    3. function Update ()
    4. {
    5.      var wantedPos = Camera.main.WorldToViewportPoint (target.position);
    6.      transform.position = wantedPos;
    7. }
     
    Recalc likes this.
  8. skaterboy

    skaterboy

    Joined:
    Jun 6, 2009
    Posts:
    25
    Hey, now it's working ......
    is it a easy way to make the text/texture not be visible over the player.
    Now it,s like a regular GUI on top of everything ....

    THANKS .......
     
  9. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    You could always make a 2d plane and have it attached to the enemy. Then have it rotate to always face the player. Then when you damage the enemy, scale down the plane to simulate it shrinking.
     
    unity_-iTbeCoo5Krs_w likes this.
  10. skoandi

    skoandi

    Joined:
    Jul 9, 2012
    Posts:
    67
    Thanks for the script! :)
     
  11. Voidexx

    Voidexx

    Joined:
    Jan 20, 2014
    Posts:
    4
    I have a Quick Question!
    haha, I made my own health bar for my character or the player that you're playing Which is right here

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyHealth : MonoBehaviour {
    5.     public int maxHealth = 100;
    6.     public int curHealth = 100;
    7.    
    8.     public float healthBarLength;
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.         healthBarLength = Screen.width / 6;
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         AddjustCurrentHealth(0);   
    18.     }
    19.    
    20.     void OnGUI() {
    21.         GUI.Box(new Rect(700, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
    22.     }
    23.    
    24.     public void AddjustCurrentHealth(int adj) {
    25.         curHealth += adj;
    26.        
    27.         if (curHealth < 0)
    28.             curHealth = 0;
    29.        
    30.         if (curHealth > maxHealth)
    31.             curHealth = maxHealth;
    32.        
    33.         if(maxHealth < 1)
    34.             maxHealth = 1;
    35.        
    36.         healthBarLength = (Screen.width / 6) * (curHealth /(float)maxHealth);
    37.     }
    38. }

    Now how would I implement your code into this to show the health above the enemy?


    THANKS IN ADVANCE
     
    Last edited: Jan 21, 2014
  12. Zakalakin

    Zakalakin

    Joined:
    Feb 5, 2014
    Posts:
    1



    Replace your GUI with this, and It'll work; c#
    Code (csharp):
    1.  
    2.     void OnGUI()
    3.     {
    4.    
    5.         Vector2 targetPos;
    6.     targetPos = Camera.main.WorldToScreenPoint (transform.position);
    7.        
    8.     GUI.Box(new Rect(targetPos.x, Screen.height- asd.y, 60, 20), health + "/" + maxHealth);
    9.        
    10.     }
    11.  
     
  13. Izzy4me

    Izzy4me

    Joined:
    Aug 14, 2014
    Posts:
    8
    I know it is old topic but i have a question about _Daniel_'s "good" simple script - how to display only one health bar? (I see another health bar if I turn around). It is possible or I should use Rect object or creating prefabs of my bar? I really enjoyed 2D bars (expecially for testing).
     
  14. Cha0os

    Cha0os

    Joined:
    Feb 22, 2014
    Posts:
    17
    _Daniel_'s script only moves a GUITexture so that it is displayed "on top" of a Target-Transform.
    So it will only display one Health Bar if that's what your GUITexture is.

    If you want to show multiple Health Bars you will have to add more GUITextures and attach the script to them.
    If you want to move the Health Bar to a different Object when certain conditions are met, you will need to update the "target"-variable accordingly.
     
  15. Izzy4me

    Izzy4me

    Joined:
    Aug 14, 2014
    Posts:
    8
    It's not exacly that I wanted to know.
    1) I created Cube1 with texture and movingScript
    2) I create new object of GUITexture and add my .png file to it (at field "Texture")
    3) GUITexture was moved in hierarchy panel to be child of Cube1 object.
    4) I created new JS script and paste that:

    Code (csharp):
    1.  
    2. var target : Transform;
    3.  
    4. function Update (){
    5.          var wantedPos = Camera.main.WorldToViewportPoint (target.position);
    6.          transform.position = wantedPos;
    7. }
    8.  
    5) I dropped script on the GUITexture object.
    6) As "Target" I choose Cube1 object.

    When I'm playing I have my bar in right place AND behind me. It's because WorldToScreenPoint ? It has problems with Y asis and duplicate texture (it's 3D game problem)?
     
  16. joandku

    joandku

    Joined:
    Aug 12, 2014
    Posts:
    3
    what is the target supposed to do it made my enemy disapear
    :cool:
     
  17. nicoguerra1337

    nicoguerra1337

    Joined:
    Nov 12, 2014
    Posts:
    3
    I found a fix for the GUI appearing from the side of the screen. Add this code in the Update() function of your script:

    (CSharp and JavaScript have the same code)

    Code (CSharp):
    1. if (renderer.isVisible)
    2.   drawGui = true;
    3. else
    4.   drawGui = false;
     
  18. KeinZantezuken

    KeinZantezuken

    Joined:
    Mar 28, 2017
    Posts:
    53
    Any ideas how to calculate position the GUI part to be "over the head" of the sprite (using Camera.main.WorldToScreenPoint() here as well)? The GO coordinates are somewhere in the middle of it, so I need to offset them but not sure how yet. Should I calculate the height of a sprite in pixels and then deduct it from y?
     
    Last edited: Jul 12, 2017
  19. unity_yclhduH3WcJO0A

    unity_yclhduH3WcJO0A

    Joined:
    Nov 7, 2018
    Posts:
    1
    lol k