Hi All,
There is a little problem that is driving me completely nuts since at least 3 hours, and no googling or forum searching got me a solution for it.
I decided to rewrite the health bar system for my game so to have them made as sprites.
I have an empty object barTransform that has two sprites as childs, one for the borders (unchanging when hit) of the health bar and one from the inner part (the part changing).
The scale of the empty container is (1,1,1) and the locale scale of the two sprites is (2.5,3,1)
Here’s the code I use to modify it when the enemy is hit
if(barTransform!=null){
float ratio = (float)(currentHealth)/(float)(maxHealth);
float saveSize = hbRenderer.bounds.extents.x;
hbInteriorTransform.localScale=new Vector3((float)(2.5f*ratio),hbInteriorTransform.localScale.y,hbInteriorTransform.localScale.z);
float diffX = (saveSize-hbRenderer.bounds.extents.x);
hbInteriorTransform.localPosition-=new Vector3(diffX/2,0f,0f);
if(currentHealth<=(maxHealth/3*2) && currentHealth>maxHealth/3){
hbRenderer.material.color = new Color(1f,1f,0f,1f);
hbInteriorTransform.localPosition-=new Vector3(diffX/2,0f,0f);
}
if(currentHealth<=maxHealth/3){
hbRenderer.material.color=new Color(1f,0f,0f,1f);
hbInteriorTransform.localPosition-=new Vector3(diffX/2,0f,0f);
}
}
It works almost correctly, but I keep on having a small gap of some pixels on the leftmost side when I try to realign it after scaling it. I am pretty sure the cause is something really stupid i don’t see, but after 3 hours i start to feel really clueless. In the pic below you see the small gap in the yellow bar, it seems to remain always identical (not proportional) while the bar shrinks. Any hint anybody?
Cheers, H
: