0
\$\begingroup\$

I know it's been asked a lot but I can't find the best solution for mine this whole day. I'm new in programming, so I have a health bar which is a child of my character which is position above it. My script is working but not the way I want it to. the local position is not decreasing as the health does and the scales size is bigger. Now I have tried several tweeks but I just could not figure it out.

Here is my code attached on the healthbar object :

 Vector3 mylocalScale;


void Start() {

    mylocalScale = transform.localScale;    
}

void Update() {
    mylocalScale.x = PlayerHealthSystem.PHS.P_Health;
    transform.localScale = mylocalScale;
}


this is the current health bar behaviour on how it it decreases which is not I want.

enter image description here


my desired result it should decrease from left to right, can anyone help me to do this and understand how to do it? thanks.
enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ How have you set up your health bar in the hierarchy and inspector? Remember, when you show us only the rendered output of your game, we have zero clue what components you're using to achieve that unless you tell us. \$\endgroup\$
    – DMGregory
    Commented May 5, 2020 at 16:12

1 Answer 1

0
\$\begingroup\$

Try updating the Origin Point of the health bar that you are scaling to be on the left of the bar. Right now the origin point is in the center (default), which causes uniform scaling on the x-axis. If you set the origin point to the left, the x-axis will scale "toward" it, giving you the intended behavior.

\$\endgroup\$
1
  • \$\begingroup\$ do you mean the pivot position of the gameObject? sorry newbie here \$\endgroup\$
    – Ryan Cat
    Commented May 5, 2020 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .