Lock object position on moving (2D)

Hello,

I have a fairly simple question for all the wizards out there.

I’m making a 2D game and I just made a health bar which floats on top of the player’s and enemies’ heads, but whenever the player moves in the opposite direction (in this case left), the health bar also moves along, basically reflecting the bar.

Does anyone know how I can potentially lock the healthbar’s position so it always faces right?

This is my current code and in case you are wondering, I have my healthbar parented to the player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealth : MonoBehaviour {

    public float max_Health = 100f;
    public float cur_Health = 0f;
    public GameObject healthBar;

    // Use this for initialization
    void Start ()
    {
        cur_Health = max_Health;
        InvokeRepeating ("decreaseHealth", 1f, 1f);
   
    }
   
    // Update is called once per frame
    void Update ()
    {

    }

    void decreaseHealth()
    {
        cur_Health -= 2f;
        float calc_Health = cur_Health / max_Health; //if cur 80 / 100 - 0.8f
        SetHealthBar (calc_Health);
    }

    public void SetHealthBar(float myHealth)
    {
        //myHealth value 0-1 ,
        healthBar.transform.localScale = new Vector3(myHealth,healthBar.transform.localScale.y,healthBar.transform.localScale.z);
    }


}

I’m assuming that your movement scripts rotate the player to “flip” them. As long as your health bar is parented to the player’s transform, it will be flipped too, which is the point of parenting transforms.

Ahh, that makes sense!
What can I do to make the health bar not flip along?

Mathf.Abs would give you the absolute value so you wont go negative.

I see, but how would I place it in the code then?

This:

healthBar.transform.localScale=newVector3(myHealth,Mathf.Abs(healthBar.transform.localScale.y),Mathf.Abs(healthBar.transform.localScale.z));

That doesn’t seem to work unfortunately. It still flips along whenever you move to the left.