Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

flip X or - scale.x

Discussion in '2D' started by GameCodeurStudent, Jan 20, 2021.

  1. GameCodeurStudent

    GameCodeurStudent

    Joined:
    Jan 13, 2018
    Posts:
    9
    Hi i want to know what is the best pratice.
    I want to flip a sprite who is a child and i dont know if i should scale the parent X to +/- value or to work on the child transform on flip it with the flipX ?
    I make try and the box collider are flipping right maybe later i will have some error like negative value.
    But i struggle to find a good answer on the 10 first google suggestion.
    Thx for the repply
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    This is what i did a long time ago and i have just kept it this way.

    Code (CSharp):
    1. private void Flip()
    2. //    {
    3. //        //Switch the way the player is labelled as facing.
    4. //        facingRight = !facingRight;
    5.  
    6. //        //Multiply the player's x local scale by -1.
    7. //        Vector3 theScale = playerGraphics.localScale;
    8. //        theScale.x *= -1;
    9. //        playerGraphics.localScale = theScale;
    10. //    }
    The Box Collider is on the Parent gameobject and this flip script works on the Graphics child object so it doesnt mess up the Box Collider. Now, if the box collider is perfectly center then it shouldnt matter as it will still be aligned.
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I pretty much never use the SpriteRenderer's flip options and always just set the scale to a +/- value.
    Reason being, if your GameObject has any child sprites/other objects that need to align a certain way with the direction of the flipped sprite, then changing the scale will keep the children aligned, while using the SpriteRenderer flip-options will not.

    For example, imagine you have some kind of animal GameObject with a few different SpriteRenderers for each body-part:
    upload_2021-1-20_23-13-7.png
    If you wanted to flip the animal to face the opposite direction, setting "flip X" on all the SpriteRenderers would just result in this:
    upload_2021-1-20_23-13-13.png
    Whereas inverting the scale of the parent GameObject would result in what you'd expect:
    upload_2021-1-20_23-13-50.png
     
    Last edited: Jan 21, 2021
  4. GameCodeurStudent

    GameCodeurStudent

    Joined:
    Jan 13, 2018
    Posts:
    9
    Ok thx for the reply.
    I found wanted to use the same solution cuz i have a shoot spwaner who need to flip as well and this one is not centered like the box collider. And i suppose the animator will run fine too. I need to make some test about this one last question.
    Thx for the reply.
    Good dev and stay safe