Display lifebar above a character

Hi all,

I'm having trouble displaying a lifebar above my character. I succeeded through reading al posts and aswers within this forum to display text above my enemies but I rather would have a livebar instead.

Therefore I combined everything I found here regarding to lifebars but I can't see how to solve this because I'm new and testing before buying ;-)

This is the code I used:

using UnityEngine;
using System.Collections;

public class HealthBar : MonoBehaviour {

    public Transform target;
    public Vector3 offset = Vector3.up;
    public bool clampToScreen = false;
    public float clampBorderSize = 0.5f;
    public bool useMainCamera = true;
    public Camera cameraToUse;

    public Texture2D texture;
    public Rect fullRect;
    public float maxHealth = 100;
    public float currHealth = 100;

    private Camera cam;
    private Transform camTransform;
    private static Vector3 thisVector;

    // Use this for initialization
    void Start () {        
        thisVector = new Vector3(transform.position.x, transform.position.y, transform.position.z);       
        if (useMainCamera)
            cam = Camera.main;
        else
            cam = cameraToUse; 
    }

    //void OnGUI()
    //{
    //    print(string.Format("Vector [{0} ; {1} ; {2}]", thisVector.x, thisVector.y, thisVector.z));   

    //    float healthFrac = currHealth / maxHealth;
    //    Rect actualRect = new Rect(thisVector.x, thisVector.y, fullRect.width * healthFrac, fullRect.height);        

    //    GUI.BeginGroup(actualRect, texture);
    //    Rect innerRect = new Rect(0, 0, fullRect.width, fullRect.height);    
    //    GUI.DrawTexture(innerRect, texture);  
    //    GUI.EndGroup(); 
    //}

    void OnRenderObject()
    {
        print(string.Format("Vector [{0} ; {1} ; {2}]", thisVector.x, thisVector.y, thisVector.z));

        float healthFrac = currHealth / maxHealth;
        Rect actualRect = new Rect(thisVector.x, thisVector.y, fullRect.width * healthFrac, fullRect.height);

        GUI.BeginGroup(actualRect, texture);
        Rect innerRect = new Rect(0, 0, fullRect.width, fullRect.height);
        GUI.DrawTexture(innerRect, texture);
        GUI.EndGroup();    
    }

    // Update is called once per frame
    void Update () {

        camTransform = cam.transform; 
        if (clampToScreen) 
        {
            Vector3 relativePosition = camTransform.InverseTransformPoint(target.position);
            relativePosition = new Vector3(relativePosition.x, relativePosition.y, Mathf.Max(relativePosition.z, 1.0f));
            thisVector = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
            thisVector = new Vector3(Mathf.Clamp(thisVector.x, clampBorderSize, 1.0f - clampBorderSize),
                                         Mathf.Clamp(thisVector.y, clampBorderSize, 1.0f - clampBorderSize),
                                         thisVector.z);
        }
        else 
        {
            thisVector = cam.WorldToViewportPoint(target.position + offset);
        }
    }
}

I see my vector print display according to me correct values but the lifebar is always drawn on the top left of the screen.

Anyone any idea what I'm missing ?

Thanks,

Wim

Ok, I fixed it!

Seems that the WorldToViewportPoint gives a normalized value back. So do draw it on the correct location you have to take the viewport itself into account by multiply the values returned with the screen.Width and screen.Height.

Thanks anyway.