1
\$\begingroup\$

How do i flip my Player in 2D when i got touch movement? I got Buttons to move but how do I flip the player now if i walk to the right cause my player faces to the left..?

here is my Player Script:

using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed = 10, jumpVelocity =  10;
    public LayerMask playerMask;
    public bool canMoveInAir = true;
    Transform myTrans, tagGround;
    Rigidbody2D myBody;
    bool isGrounded = false;

    // Use this for initialization
    void Start () 
    {
        myBody = this.GetComponent<Rigidbody2D>();
        myTrans = this.transform;   
        tagGround = GameObject.Find (this.name + "/tag_ground").transform;
    }

    // Update is called once per frame
    void FixedUpdate () 
    {
        isGrounded = Physics2D.Linecast (myTrans.position, tagGround.position, playerMask);

        #if UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
        Move(Input.GetAxisRaw("Horizontal"));   
        if(Input.GetButtonDown("Jump"))
           Jump();
        #endif
    }

    public void Move(float horizontalInput) 
    {
        //Er klebt nicht mehr an den Waenden
        if (!canMoveInAir && !isGrounded)
            return;

        Vector2 moveVel = myBody.velocity;
        moveVel.x = horizontalInput * speed;
        myBody.velocity = moveVel;  


    }

    public void Jump()
    {
        if(isGrounded)
            myBody.velocity += jumpVelocity * Vector2.up;
    }

}
\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

Flipping object through can be done through scaling. If you set scale to -1 in X then it'd be flipped in X axis, similar for other axes.

You can just use your horizontalInput to determine if you are going left or right, and obviously if your horizontalInput is negative then your going left and vice versa.

public void Move(float horizontalInput) 
{
    ...
    // Here is the flipping stuff
    if (horizontalInput != 0)
    {
        Vector3 flip = Vector3.right * horizontalInput * myTrans.localScale.x;
        myTrans.localScale = flip;
    }
}

As you taking horizontalInput with Input.GetAxisRaw means with no smoothing filter so I did multiply it directly, but if you change it whenever then you have to make some change in code also, what you have to do is just change

Vector3 flip = Vector3.right * horizontalInput * myTrans.localScale.x;

To

Vector3 flip = (Vector3.right * horizontalInput).normalized * myTrans.localScale.x;
\$\endgroup\$
3
  • \$\begingroup\$ i added the Code but now it resizes my character to 0 and it fall trough the ground, my player Scale is 25x and 25y what should i do now ? \$\endgroup\$ Commented Mar 20, 2016 at 12:24
  • \$\begingroup\$ Check the edits \$\endgroup\$ Commented Mar 20, 2016 at 13:15
  • \$\begingroup\$ You should not hard code like that \$\endgroup\$ Commented Mar 20, 2016 at 13:31
0
\$\begingroup\$

You can simply do GetComponent<SpriteRenderer>().flipX = true;

\$\endgroup\$
-1
\$\begingroup\$

Disclaimer: as Valryon said, this is not the right way to do that anymore.

Your Question is not clear, but I think what you want to do is flip the entire GameObject.
To do this, simply do myTrans.localScale *= -1 when the player changes direction.

You can also do something like this:

if(horizontalInput <  0)
    myTrans.localScale = new Vector2(1, 1, 1)
if(horizontalInput >  0)
    myTrans.localScale = new Vector2(-1, -1, -1)

at the end of the Move(float horizontalInput) function.

\$\endgroup\$
4
  • \$\begingroup\$ I just get an Error if i try ur Code.. and yes i wanna change the direction of the GameObject \$\endgroup\$ Commented Mar 19, 2016 at 23:18
  • \$\begingroup\$ okey i got it u just used this Code: if (horizontalInput < 0) { transform.localScale = new Vector2 (25, 25); } if (horizontalInput > 0) { transform.localScale = new Vector2 (-25, 25); } \$\endgroup\$ Commented Mar 20, 2016 at 13:23
  • \$\begingroup\$ Yes, of course you need to adjust the values to the scale you want your GameObject to be. \$\endgroup\$
    – Stolous
    Commented Mar 20, 2016 at 14:59
  • \$\begingroup\$ I was also using this technique, but I discovered that using scale to flip is discouraged since Unity 5.4 (it logs a warning on the colliders). The appropriate way is now the flipX boolean on the SpriteRenderer. \$\endgroup\$
    – Valryon
    Commented Mar 16, 2017 at 20:07

You must log in to answer this question.

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