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.

Health Bar above ememy

Discussion in 'Scripting' started by JasonWIR, Mar 12, 2011.

  1. JasonWIR

    JasonWIR

    Joined:
    Mar 7, 2011
    Posts:
    84
    I can make a health bar show up at a static place at the a predefined top and left but I want it to show up above my enemy.

    I added the script to my enemy, but I can't figure out how to get my enemy's top and left on the screen to put the health bar above him.

    Can someone help?

    Thanks!
     
  2. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Yea you can use the camera to convert the players position to screen coordinates and then use that to calculate your Rect for GUI.

    so it would look something like this ( do check if Camera.current is non null )
    Code (csharp):
    1.  
    2.      Vector3 screenPosition =
    3.            Camera.current.WorldToScreenPoint(transform.position);// gets screen position.
    4.      screenPosition.y = Screen.height - (screenPosition.y + 1);// inverts y
    5.      Rect rect = new Rect(screenPosition.x - 50,
    6.            screenPosition.y - 12, 100, 24);// makes a rect centered at the player ( 100x24 )
    7.      GUI.Box(rect, "Enemy");
    8.  
    Thats how you would go about getting the rect anyways.
     
  3. JasonWIR

    JasonWIR

    Joined:
    Mar 7, 2011
    Posts:
    84
    That works, but creates additional health bars on the other side of the view. When my back it to the enemy, there is also another health bar hovering over nothing.
     
    Last edited: Mar 12, 2011
  4. JasonWIR

    JasonWIR

    Joined:
    Mar 7, 2011
    Posts:
    84
    Somehow I need to check to see if the enemy is in sight.
     
  5. dee

    dee

    Joined:
    Jun 4, 2010
    Posts:
    36
    Try something like this:
    Code (csharp):
    1.  
    2. private var draw : boolean=true;
    3. function OnDrawGUI(){
    4.  if (!draw) return;
    5.  // > Healthbar code<
    6. }
    7. function OnBecameVisible () {
    8.  draw= true;
    9. }
    10.  
    11. function OnBecameInvisible () {
    12.  draw= false;
    13. }
    14.  
    15.  
     
  6. JasonWIR

    JasonWIR

    Joined:
    Mar 7, 2011
    Posts:
    84
    Worked great! Thanks
     
  7. 3uhox

    3uhox

    Joined:
    Jun 4, 2011
    Posts:
    61
    thank you, it works great
     
  8. Bekkk

    Bekkk

    Joined:
    Jul 28, 2012
    Posts:
    17
    I have the same problem with additional health bars on the other side of the view. Anyone knows how to solve it please?
     
  9. modulo

    modulo

    Joined:
    Sep 10, 2011
    Posts:
    31
    try this script

     
    Last edited: Aug 8, 2012
  10. Nagas

    Nagas

    Joined:
    Jul 5, 2012
    Posts:
    24
    Great Topic! Great Ideas!!!! Helped a lot!!!

    THANKS!!!!!
     
  11. Jacques3d

    Jacques3d

    Joined:
    Nov 6, 2012
    Posts:
    4
    JasonWIR show me your code please, i have tried this, but it doesn't worked for me.
     
  12. Xio Rheims

    Xio Rheims

    Joined:
    Aug 7, 2013
    Posts:
    1
    Modified the code to show a different kind of health bar, and then some text indicating the characters HP. Also I added a raycast so if a character is behind an object his HP wont show up. The script just needs to be attached to the character you want to have a health bar, and maybe some other minor changes in the script tailored to your game.

    Code (csharp):
    1. var health : float;
    2. var maxHealth : float;
    3.  
    4. var adjustment : float= 2.3f;
    5. private var worldPosition : Vector3= new Vector3();
    6. private var screenPosition : Vector3= new Vector3();
    7. private var myTransform : Transform;
    8. private var myCamera : Camera;
    9. private var healthBarHeight : int= 5;
    10. private var healthBarLeft : int= 110;
    11. private var barTop : int= 1;
    12. private var myStyle : GUIStyle= new GUIStyle();
    13.  
    14.  
    15. //assign the camera to a variable so we can raycast from it
    16. private var myCam : GameObject;
    17. myCam = GameObject.Find("MainCamera"); //I removed the space from the camera's name in the Unity Inspector, so you will probably need to change this
    18.  
    19. function Awake()
    20. {
    21.     myTransform = transform;
    22.     myCamera = Camera.main;
    23.     health = 50; //arbritrarily chosen values to show that this script works
    24.     maxHealth = 100;
    25. }
    26.  
    27. function OnGUI()
    28. {
    29.     worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustment,myTransform.position.z);
    30.     screenPosition = myCamera.WorldToScreenPoint(worldPosition);
    31.    
    32.     //creating a ray that will travel forward from the camera's position   
    33.     var ray = new Ray (myCam.transform.position, transform.forward);
    34.     var hit : RaycastHit;
    35.     var forward = transform.TransformDirection(Vector3.forward);
    36.     var distance = Vector3.Distance(myCam.transform.position, transform.position); //gets the distance between the camera, and the intended target we want to raycast to
    37.    
    38.     //if something obstructs our raycast, that is if our characters are no longer 'visible,' dont draw their health on the screen.
    39.     if (!Physics.Raycast(ray, hit, distance))
    40.     {
    41.         GUI.color = Color.red;
    42.         GUI.HorizontalScrollbar(Rect (screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop, 100, 0), 0, health, 0, maxHealth); //displays a healthbar
    43.        
    44.         GUI.color = Color.white;
    45.         GUI.contentColor = Color.white;                
    46.         GUI.Label(Rect(screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop+5, 100, 100), ""+health+"/"+maxHealth); //displays health in text format
    47.     }
    48. }
     
    dilettant likes this.
  13. Gibras

    Gibras

    Joined:
    Jan 8, 2014
    Posts:
    1
    Thanks!!!

    An entire night wasted trying to figure out how to make this, and then i found this Thread.

    Great Topic!:D
     
unityunity