For your first question, try creating an empty gameObject above your capsule at the point you want your health bar, and base the health bar’s position on that object (you’d have to change target to the new object).
var target : GameObject;
var texture : Texture;
var offsetY : float = 60;
var offsetX : float = 40;
function Update (){
}
function OnGUI() {
if (Vector3.Distance(transform.position, target.transform.position) < 15 target.renderer.isVisible){
var viewPos : Vector2 = camera.main.WorldToScreenPoint (target.transform.position);
GUI.DrawTexture(new Rect(viewPos.x - offsetX, Screen.height - (viewPos.y + offsetY), 75, 20), texture);
}
}
But now I wondered how to find all targets (enemies) in the scene and draw their hp bars. I tried different ways but allways got an errors, can any1 help me here?