简体   繁体   中英

Unity 2D UI element direction changes according to its game object direction

So I've added a health bar below the main player, following a tutorial. I had a canvas with render mode set to "World Space". I added the UI elements for the healthbar to the canvas. Then I made the canvas a child of the player Mario. Now the healthbar follows the player. The problem is that whenever Mario changes direction on the x axis his sprite changes direction but also does the healthbar, because in Mario's script the localscale changes according to the direction of the player. Any ideas?

Mario's script:

public class MarioMove : MonoBehaviour
{

private Rigidbody2D rb;
private Animator anim;
private float moveSpeed;
private float dirX;
private bool facingRight = true;
private Vector3 localScale;
private bool doubleJumped;
[SerializeField] HealthBar healthbar;

 private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        localScale = transform.localScale;
        moveSpeed = 5f;
    }

private void Update()
    {

        if (Input.GetKeyDown(KeyCode.DownArrow)) {
            PlayerTakeDamage(20);
        }
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            PlayerHeal(20);
        }

    dirX = Input.GetAxisRaw("Horizontal") * moveSpeed;

        if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
            rb.AddForce(Vector2.up * 400f);

        if (Mathf.Abs(dirX) > 0 && rb.velocity.y == 0)
            anim.SetBool("isRunning", true);
        else
            anim.SetBool("isRunning", false);

    if (rb.velocity.y == 0)
    {
        
        anim.SetBool("isJumping", false);
        anim.SetBool("isFalling", false);
        doubleJumped = false;
    }

        if (rb.velocity.y > 0 && !doubleJumped)
        {
            
            if (Input.GetButtonDown("Jump")){
                anim.SetBool("isDoubleJumping", true);
                rb.AddForce(Vector2.up * 100f);
                anim.SetBool("isJumping", false);
                doubleJumped = true;
            }
            else anim.SetBool("isJumping", true);

        }

    if (rb.velocity.y > 0 && doubleJumped)
    {
        anim.SetBool("isDoubleJumping", true);
    }

    if (rb.velocity.y < 0)
        
        {
            if (Input.GetButtonDown("Jump") && !doubleJumped)
            {
                anim.SetBool("isDoubleJumping", true);
                rb.velocity = Vector2.zero;
                rb.AddForce(Vector2.up * 200f);
                anim.SetBool("isFalling", false);
                doubleJumped = true;
            }
            else {
                anim.SetBool("isJumping", false);
                anim.SetBool("isDoubleJumping", false);
                anim.SetBool("isFalling", true);
            }
        
        }

    }    

private void FixedUpdate()
    {
        rb.velocity = new Vector2(dirX, rb.velocity.y);
    }

private void LateUpdate()
    {
    if (dirX > 0)
        facingRight = true;
    else if(dirX < 0)
        facingRight = false;


    if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
            localScale.x *= -1;

        transform.localScale = localScale;
    }

private void PlayerTakeDamage(int damage)
{
    GameManager.gameManager.playerHealth.DamageUnit(10);
    healthbar.SetHealth(GameManager.gameManager.playerHealth.Health);
}

private void PlayerHeal(int healing)
{
    GameManager.gameManager.playerHealth.HealUnit(10);
    healthbar.SetHealth(GameManager.gameManager.playerHealth.Health);
}

}

I solved by making the scaling of the UI independent of the scaling of the Mario object (made the mario and u elements different children of a game object and applied different transformations to them that are independent to each other).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM