Sprite healthbar

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


:

Apparently it was stupid indeed. If anybody else ends up in a similar problem, here’s the solution

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);
                if(currentHealth>(maxHealth/3*2)){
                    hbInteriorTransform.localPosition-=new Vector3(diffX,0f,0f);
                }
                else if(currentHealth<=(maxHealth/3*2) && currentHealth>maxHealth/3){
                    hbRenderer.material.color = new Color(1f,1f,0f,1f);
                    hbInteriorTransform.localPosition-=new Vector3(diffX,0f,0f);
                }
                else if(currentHealth<=maxHealth/3){
                    hbRenderer.material.color=new Color(1f,0f,0f,1f);
                    hbInteriorTransform.localPosition-=new Vector3(diffX,0f,0f);
                }
            }

Hi man,

I see that you got it working, but just to give another option:

Where is the pivot point of your sprite? If it’s in the center it will be more difficult to scale it, but if you put the pivot on one of the sides like (0,0.5) you will just have to change the scale of the object and it will stay on the right spot…

I suddenly feel so dumb…one year working with sprites and i ignored that it was possible to change the pivot.
and at least i can also skip one translation.
thanks, good thing to know!