When i 'm attacking the mob it’s health bar doesnt update.In same time he’s attacking me , my healthbar is updating properly.
Mob’s health update after i select it again with TAB.
Here’s my vitalbars class.
/// <summary> /// VitalBar.cs /// This class is resposible for displaying a vital for the palyer or amob.
///
using UnityEngine;
using System.Collections;public class VitalBar : MonoBehaviour { public bool _isPlayerHealthBar; //tells if its ours hp bar or mob'shpbar.
private int _maxBarLength; //max length of bar when hp is 100% private int _curBarLength; //current length of hp bar. private GUITexture _display; void Awake(){ _display = gameObject.GetComponent<GUITexture>(); } // Use this for initialization void Start () { //_isPlayerHealthBar = true; _maxBarLength = (int)_display.pixelInset.width; OnEnable(); } // Update is called once per frame void Update () { } //This method is called when the gameobject is enabled. public void OnEnable(){ if(_isPlayerHealthBar){ Messenger<int, int>.AddListener("player healthupdate", OnChangeHealthBarSize);
} else{ ToggleDisplay(false); Messenger<int, int>.AddListener("mob health update",OnChangeHealthBarSize);
Messenger.AddListener(“show
mob vitalbars”, ToggleDisplay);} } //This method is called when the gameobject is disabled. public void OnDisable(){ if(_isPlayerHealthBar) Messenger<int, int>.RemoveListener("player healthupdate", OnChangeHealthBarSize);
else {
Messenger<int, int>.RemoveListener(“player health
update”, OnChangeHealthBarSize);
Messenger.RemoveListener(“show
mob vitalbars”, ToggleDisplay);} } //calculating the total size of the hpbar in relaion to the % ofhealth the target had left.
public void OnChangeHealthBarSize(int curHealth,
int maxHealth){
//Debug.Log(“we heard:curHealth = " + curHealth + " -maxHealth =” + maxHealth);
_curBarLength = (int)((curHealth / (float)maxHealth) * _maxBarLength);
//_display.pixelInset = new Rect(_display.pixelInset.x,
_display.pixelInset.y, _curBarLength, _display.pixelInset.height);
_display.pixelInset = CalculatePosition();
}
//setting the hpbar to the player or mob.
public void SetPlayerHealth(bool b){
_isPlayerHealthBar = b;
}private Rect CalculatePosition(){ float yPos = _display.pixelInset.y /2 -10; if(!_isPlayerHealthBar){ float xPos = (_maxBarLength - _curBarLength) - (_maxBarLength /4+10); return new Rect(xPos, yPos, _curBarLength, _display.pixelInset.height); } return new Rect(_display.pixelInset.x, yPos,_curBarLength, _display.pixelInset.height);
}
private void ToggleDisplay(bool show){
_display.enabled = show;
}}