Search Unity

How to flip animation in 2d?

Discussion in '2D' started by Son_David, Dec 24, 2015.

?

How long have you been using Unity3d?

Poll closed Dec 31, 2015.
  1. More than 5 years

    12.5%
  2. 1-2 years

    37.5%
  3. I just started!

    50.0%
  1. Son_David

    Son_David

    Joined:
    Dec 24, 2015
    Posts:
    1
    hello everyone,

    I am working on a 2d platformer game prototype and I am having trouble animating my player across the screen. He faces right when I press the right arrow key to move him. When I press the left key, he is still faces right but moves in the opposite direction. I can't seem to make him face to the left. His animations are very simple to make because he doesn't have any legs. Is there a solution to this? I would greatly appreciate it. I hope to show off my progress once I’m done.
     
  2. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    You could set the transform.localScale.x to -1 when facing left, and to 1 when facing right. I've also noticed that SpriteRenderer has an option to flip a sprite on the x or y axis, so that might be an option as well.
     
    Cristinel01, abtlb76 and twsbi like this.
  3. Prototypetheta

    Prototypetheta

    Joined:
    May 7, 2015
    Posts:
    122
    You could also rotate your sprite 180 on the y axis. Which does literally the same thing.
     
    ratio_l likes this.
  4. SolarPetal

    SolarPetal

    Joined:
    Nov 7, 2015
    Posts:
    11
    You could use a boolean and a function to do this (It's pretty simple actually)
    Code (CSharp):
    1. public bool facingRight = true; //Depends on if your animation is by default facing right or left
    2.  
    3. void FixedUpdate()
    4.     {
    5.         float h = Input.GetAxis("Horizontal");
    6.         if(h > 0 && !facingRight)
    7.             Flip();
    8.         else if(h < 0 && facingRight)
    9.             Flip();
    10.      }
    11. void Flip ()
    12.     {
    13.         facingRight = !facingRight;
    14.         Vector3 theScale = transform.localScale;
    15.         theScale.x *= -1;
    16.         transform.localScale = theScale;
    17.     }
    18.  
     
  5. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    upload_2015-12-24_15-27-27.png
    there is a flip checkbox on sprite (but not image?) renderer in the latest versions
     
    voncarp likes this.
  6. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    how to flip fbx animations in unity
     
  7. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140


    this will work for sprite but for animation i used this code
    void OnTriggerStay2D(Collider2D other)
    {
    Debug.Log ("TriggerStart");
    if (other.gameObject.tag == "Archer") {
    foreach (Collider2D collider in other.gameObject.GetComponents<Collider2D>())
    {

    collider.isTrigger = true;
    if (collider is BoxCollider2D) {
    BoxCollider2D myBoxCollider = (BoxCollider2D)collider;
    archer.transform.Rotate(new Vector3(0, -180, 0));

    Debug.Log ("Flip");
    //Do some stuff with the box collider
    }
    else if (collider is CircleCollider2D) {
    CircleCollider2D myCircleCollider = (CircleCollider2D)collider;

    //Do some stuff with the cyrcle collider

    }
    }
    }
    but this is not working can u help me to solve??
     
    unity_BDcSxrZk94spmw likes this.
  8. jjjoa

    jjjoa

    Joined:
    Aug 1, 2017
    Posts:
    2
  9. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
  10. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    It's an old question, but probably newbies like me will be asking still. So here is my solution:

    First I get the original scale of the game object at Start():
    Code (CSharp):
    1. scale = this.gameObject.transform.localScale;
    Then I do the magic in the FixedUpdate() altogether with the movement. The booleans "movDir" and "movEsq" are set n Update() when I get the keyboard commands of "x" movement of the gameobject.
    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         if (movDir)
    4.         {
    5.             movMov = 1;
    6.             this.gameObject.transform.localScale = scale;
    7.         }
    8.         else if (movEsq)
    9.         {
    10.             movMov = -1;
    11.             this.gameObject.transform.localScale = new Vector3(-1 * scale.x, scale.y, scale.z);
    12.         }
    13.         else
    14.         {
    15.             movMov = 0;
    16.         }
    17.  
    18.         if (pulo && movMov != 0) this.gameObject.transform.Translate(new Vector3(movMov * movMax, 0, 0));
    19.     }
    I tried using rotation 180 degrees but didn't work, don't know why there is no mirroring as negative scale.
     
    laorient likes this.
  11. unity_72NNT-e0FQsyKw

    unity_72NNT-e0FQsyKw

    Joined:
    Feb 22, 2021
    Posts:
    13
    I think what people are looking for is making an animation follow a mouse input. Isn't there a way to create an animation and just have the animation orient towards an input? You have an animation of a ball moving right, but if you rotate the gameobject so right is now up, then the animation would still play right, but look as if it's going up.

    Great for directional melee combat for a top down 2d game
     
  12. arkadiuszmazurek13

    arkadiuszmazurek13

    Joined:
    Jun 29, 2020
    Posts:
    1
    My solution:


    Code (CSharp):
    1.     private void RotateAnimation()
    2.     {
    3.         if (Input.GetAxis("Horizontal") > 0.01f)
    4.             gameObject.GetComponent<SpriteRenderer>().flipX = false;
    5.         else if (Input.GetAxis("Horizontal") < -0.01f)
    6.             gameObject.GetComponent<SpriteRenderer>().flipX = true;
    7.     }
     
    Sylvain91210 likes this.