How to make the Health bar on Enemys head not be in relation to Player(main) Camera?

I have a Rectangle working as a Health bar for my enemies. The bar always starts in the middle of the Enemies’ x position, however the y position is in relation to the player so when i jump the health bar jumps with me. I want the health bar to be on top, and in the middle of the enemies however. (See picture)
My Camera is a child of the Player.
Here’s how i position the Rectangle:

void OnGUI()
    		Vector2 targetPos;
    		targetPos = RectTransformUtility.WorldToScreenPoint(Camera.main, transform.position);
    		healthRect = new Rect (targetPos.x, targetPos.y, Screen.width / 10, Screen.height / 50);
    		float ratio = health / maxHealth;
    		float rectWidth = ratio*Screen.width / 10;
    		healthRect.width = rectWidth;
    		GUI.DrawTexture (healthRect, healthTexture);

What is this script attached to?
This script should be attached to every enemy gameObject right?

You realize that in your code your setting up the rectangle width and height to be /10 or /50 of the screen. So on different size screens you will be getting much larger or smaller size of health bar? Where the character size stays the same…was this intended?

  1. Center the X position – I would make this a constant instead of Screen.width /10. But regardless, You would center it by determining how wide your rectangle is…and then dividing it by two. So because the width of this healthbar is Screen.width/2… You would set your X position as: targetPos.x - (Screen.width /2)

  2. Center the Y position – I think I may have figured out your problem with the Y coordinates… The ONGUI functions has a different coordinate layout than Camera.WorldToScreenPoint…Once you got this figured out, you can simply center the Y where it needs to be by adjusting it with an adjusting variable… like this:

IT WONT LET ME USE THE DAMN CODE BOX: so here it is in regular text format:
//setup a public adjuster, so that you can double check the reference height from the editor and adjust it while the game is running…

public int adjusterY;

//Then just add this number to your Y positioning when you define the drawbox
healthRect = new Rect (targetPos.x, targetPos.y + ADJUSTERY, Screen.width / 10, Screen.height / 50);

**On GUI the X,Y = 0,0 starts at the BOTTOM LEFT of the screen
**However, the WorldToScreen pixels x,y = 0,0 starting at the TOP LEFT of the screen.

To put the world-screen pixels into the GUI format

//invert the coordinate system since GUI is BOTTOM,LEFT while Coordinates are TOP,LEFT
targetPos.y = Camera.main.pixelHeight - targetPos.y;

Let me know if this helped anything…

Unity versions from 4.6 to newest one doesn’t use OnGUI(). Instead they use Canvas & other UI elements, who are simpler for use, for me personaly. I recommend you to start here:

If you are really new to Unity, I recommend also Unity tutorials, they are great, and have all explained

For your specific problem, I use this code:

//I am not too good in c#, i prefer js, so there could be some mistakes

using UnityEngine;
using System.Collections;
using System.UI;

public class EnemyHealthBar : MonoBehaviour {
UI.Image healthBar;
    void  Update (){
           //get coordinates of enemy in 2D plane
            Vector2 enemyCoordinates = Camera.main.WorldToScreenPoint(transform.position);
            //and get it to top of enemy head
            //change position of healthBar, and his fillAmount
        	healthBar.rectTransform.position = screenCoordinates;
        	healthBar.fillAmount = enemyHealth/100;