The Healtbar draws a green(1/1)/orange (1/2)/red(1/4) bar for the hitpoints and over that a blue armor bar (that it looks like it is protecting the hitpoints). It is surrounded by a black line. The script has a SizeInPixels property (set by 50px) - that's the width of the bar wich currently does NOT change depending on the distance.
Its just for debugging. But nice, though.
Usage
- Add the script to the Unit that you want to observe.- Add an empty GameObject to the same Unit, and position it at the center of where the HealthBar should be displayed.
- Drag the created GameObject on the Position property of the HealthBar script.
Before you can run it, you will have to meet the requirements below.
Fast download including tex_WHITE --> HERE
Requirements
The script requires another script named DamageReceiver with the floats hitPoints, maxHitPoints, armorPoints and maxArmorPoints.
Also you need a blank texture with white as background color, named tex_WHITE wich MUST be placed in the Resources Folder. If it does not exist, create it.
The bold stuff MUST be named exactly like this. But you can manage the damage code like you want.
(My DamageReceiver will be posted later on.)
Script
public var SizeInPixels:float=50;
private var fov = 60.0; // the field of view. If you remove this,
// the healthbars will also be drawn "on the opposite"
// of the unit from camera view.
private var hit : RaycastHit;
function inLineOfSight () : boolean {
if (Vector3.Angle(gameObject.transform.position - Camera.main.transform.position, Camera.main.transform.forward) <= fov &&
Physics.Linecast(Camera.main.transform.position, gameObject.transform.position, hit))
{
// linecast hits the gameobject
if(hit.collider.transform == gameObject.transform)
return true;
// (else) it could also be a child (of the gameobject) wich was hit by the linecast.
for(var child:Transform in gameObject.transform)
{
if(hit.collider.transform == child)
return true;
}
}
return false;
}
function OnGUI()
{
if(inLineOfSight())
{
// get 2D position of the drawing position on the screen
var screenPosition = Camera.main.WorldToScreenPoint(Position.position);
// get the white texture
var tWHITE : Texture2D=Resources.Load("tex_WHITE") as Texture2D;
// get variables from the DamageReceiver, wich must be added to the same object.
var component:Component = gameObject.GetComponent(DamageReceiver);
var hitPoints=component.hitPoints;
var maxHitPoints=component.maxHitPoints;
var armorPoints=component.armorPoints;
var maxArmorPoints=component.maxArmorPoints;
//Set color to black and draw the background bar
GUI.color = Color.black;
GUI.DrawTexture(new Rect(screenPosition.x-1-SizeInPixels/2,Screen.height-screenPosition.y-1,SizeInPixels+2,4), tWHITE);
// now set the color depending on the health and draw a bar with this color.
GUI.color=Color.green;
if(hitPoints<maxHitPoints/2)
GUI.color = new Color(1.0f , 0.5f, 0);//Set color to Orange
if(hitPoints<=maxHitPoints/4)
GUI.color = Color.red; //Set color to red
GUI.DrawTexture(new Rect(screenPosition.x-SizeInPixels/2,Screen.height-screenPosition.y,(SizeInPixels/maxHitPoints)*hitPoints,2), tWHITE);
// lastly draw the armor bar over all the other stuff (looks like its protecting, not?)
GUI.color = new Color(0.2f,0.2f,1.0f);//set color to bright blue
GUI.DrawTexture(new Rect(screenPosition.x-SizeInPixels/2,Screen.height-screenPosition.y,(SizeInPixels/maxArmorPoints)*armorPoints,2), tWHITE);
GUI.color=Color.white;
}
}
@script RequireComponent(DamageReceiver)
private var fov = 60.0; // the field of view. If you remove this,
// the healthbars will also be drawn "on the opposite"
// of the unit from camera view.
private var hit : RaycastHit;
function inLineOfSight () : boolean {
if (Vector3.Angle(gameObject.transform.position - Camera.main.transform.position, Camera.main.transform.forward) <= fov &&
Physics.Linecast(Camera.main.transform.position, gameObject.transform.position, hit))
{
// linecast hits the gameobject
if(hit.collider.transform == gameObject.transform)
return true;
// (else) it could also be a child (of the gameobject) wich was hit by the linecast.
for(var child:Transform in gameObject.transform)
{
if(hit.collider.transform == child)
return true;
}
}
return false;
}
function OnGUI()
{
if(inLineOfSight())
{
// get 2D position of the drawing position on the screen
var screenPosition = Camera.main.WorldToScreenPoint(Position.position);
// get the white texture
var tWHITE : Texture2D=Resources.Load("tex_WHITE") as Texture2D;
// get variables from the DamageReceiver, wich must be added to the same object.
var component:Component = gameObject.GetComponent(DamageReceiver);
var hitPoints=component.hitPoints;
var maxHitPoints=component.maxHitPoints;
var armorPoints=component.armorPoints;
var maxArmorPoints=component.maxArmorPoints;
//Set color to black and draw the background bar
GUI.color = Color.black;
GUI.DrawTexture(new Rect(screenPosition.x-1-SizeInPixels/2,Screen.height-screenPosition.y-1,SizeInPixels+2,4), tWHITE);
// now set the color depending on the health and draw a bar with this color.
GUI.color=Color.green;
if(hitPoints<maxHitPoints/2)
GUI.color = new Color(1.0f , 0.5f, 0);//Set color to Orange
if(hitPoints<=maxHitPoints/4)
GUI.color = Color.red; //Set color to red
GUI.DrawTexture(new Rect(screenPosition.x-SizeInPixels/2,Screen.height-screenPosition.y,(SizeInPixels/maxHitPoints)*hitPoints,2), tWHITE);
// lastly draw the armor bar over all the other stuff (looks like its protecting, not?)
GUI.color = new Color(0.2f,0.2f,1.0f);//set color to bright blue
GUI.DrawTexture(new Rect(screenPosition.x-SizeInPixels/2,Screen.height-screenPosition.y,(SizeInPixels/maxArmorPoints)*armorPoints,2), tWHITE);
GUI.color=Color.white;
}
}
@script RequireComponent(DamageReceiver)
Keine Kommentare:
Kommentar veröffentlichen