Hello, I am working on my enemy health bars. They are sprites and children to the enemy game object. There is one red bar and one black bar. To start they are the same size and at the same position. When an enemy gets hit the red bar decreases in scale on the x. So far it decreases in size correctly when attacked. The problem is that when I change the size of the red bar it no longer is aligned with the black bar to the left. This leaves black areas on both the left and right side. I would like the two bars to keep the same x position so the lost health appears on the right side. This is what I thought would accomplish this.
private GameObject healthBar;
private Transform blackHealthBar;
....
public void takeDamage(float damage){
curHealth -= damage;
if (curHealth <= 0) {
Destroy(gameObject);
}
float healthRatio = curHealth / originalHealth;
healthBar.transform.localScale = new Vector3 (originalHealthBarX * healthRatio, healthBarYScale,
healthBarZScale);
healthBar.transform.position = new Vector3 (blackHealthBar.position.x,
healthBar.transform.position.y,
healthBar.transform.position.z);
}
I thought that changing the red health bar’s x position to the black health bar’s x position would solve this. Both of the x positions are the same even after changing the scale of the red bar. Does anyone know how I can resolve this issue or know a different approach?
Brilliant! Thank you works perfect! I'd up-vote your answer but I don't have enough rep!
– andrew_csSo clever! +1 I don't have enough rep to up vote ¬¬
– pjnovasGreat, thanks ! You forgot a ) before the ; on the last line :)
– nicmarxp