Hey, I’m using a gui texture for a health bar and having it hover above an enemy when I mouse over it. I’m trying to get it to disappear when the mouse comes away but it it seems to glitch out and stick to the screen.
I’ve literally being trying to solve this for the past 3-4 hours and I can’t figure it out for the life of me. I’m using two JS. Both are attached to the enemy called EnemyHealth1 and the other is EnemyMouseOver.
Could anyone tell me a better way of doing this or something similar?
All I want is for the healthbar to show up when I mouse over then fade o.0
Here is my EnemyHealth1 Script
var hp:float;
var maxHp:float;
var healthBarWidth:int;
var myHealthBar:GameObject;
var myHb:GameObject;
var posX:float;
var posY:float;
function Start () {
healthBarWidth = 20;
myHb = Instantiate(myHealthBar, transform.position, transform.rotation);
}
function Update () {
myHb.transform.position = Camera.main.WorldToViewportPoint(transform.position);
myHb.transform.position.x -= posX;
myHb.transform.position.y -= posY;
myHb.transform.localScale = Vector3.zero;
var healthPercent:float = hp / maxHp;
if(healthPercent < 0){
healthPercent = 0;
}
if(healthPercent > 100){
healthPercent = 100;
}
healthBarWidth = healthPercent * 20;
myHb.guiTexture.pixelInset = Rect (10, 10, healthBarWidth, 5);
}
This is the EnemyMouseOver
var healthEnabled = false;
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, 10))
{
if(hit.collider.gameObject.tag == "Enemy")
{
if (healthEnabled == false)
{
gameObject.GetComponent(EnemyHealth1).enabled = true;
healthEnabled = true;
}
}
}
else
{
gameObject.GetComponent(EnemyHealth1).healthBarWidth = 0;
if (healthEnabled == true){
gameObject.GetComponent(EnemyHealth1).healthBarWidth = 0;
gameObject.GetComponent(EnemyHealth1).enabled = false;
healthEnabled = false;
}
}
}