- Home /
My 2D Platformer Game, my HealthBar Flips Left when I move Player Left But is Right Direction when goes right.
using UnityEngine; using UnityEngine.UI; public class StatusIndicator : MonoBehaviour { [SerializeField] private RectTransform healthBarRect; [SerializeField] private Text healthText;
void Start()
{
if (healthBarRect == null)
{
Debug.LogError("STATUS INDICATOR: No health bar object referenced!");
}
if (healthText == null)
{
Debug.LogError("STATUS INDICATOR: No health text object referenced!");
}
}
public void SetHealth(int _cur, int _max)
{
float _value = (float)_cur / _max;
healthBarRect.localScale = new Vector3(_value, healthBarRect.localScale.y, healthBarRect.localScale.z);
healthText.text = _cur + "/" + _max + "HP";
}
}
This is the script my students are using...
Is your health bar a child of the player character?
Answer by jvernon · Mar 01, 2021 at 11:45 PM
The health bar is a child of the status indicator, but the status indicator is childers to the player*
Answer by $$anonymous$$ · Mar 02, 2021 at 12:46 AM
The problem is the status indicator is a child to the player. So when the player turns left it rotates 180 degrees as well.
You have two ways to fix this.
Do not rotate Player, instead just use animation.
Unparent the status indicator from the player and just let it move with it through script