• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by bpioske · Apr 22, 2015 at 04:23 PM · 2dsprite

How to flip sprite horizontally in Unity 2D?

I posted a question a few days ago, but haven't received any responses. I thought I would rephrase my question in hopes of getting some better feedback.

I'm trying to understand my options for flipping sprites horizontally in Unity 2D (specifically version 5.0+). From what I know, there are two main options:

  1. Create sprites for facing right and facing left and switch between them as needed

  2. Create a single sprite for facing one direction and multiply the transform's localScale by -1 whenever you want to mirror the image

I was using option #2 in Unity 4.X without any problems, but when I upgraded my project to unity 5.0, modifying the localScale made my sprite disappear. Any ideas why that might be the case? Please see the code in question below. If anyone has some efficient alternatives, please let me know.

 // Flip sprite / animation over the x-axis
      protected void Flip()    
      {
          isFacingRight = !isFacingRight;
  
          Vector3 theScale = transform.localScale;
          theScale.x *= -1;
          transform.localScale = theScale;
  
          // Flip collider over the x-axis
          center.x = -center.x;
      }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image davsanmon · Jul 25, 2016 at 04:15 PM 0
Share

And the animations that are in the sprite, or in the objetc that contains the sprite in the hierarchy, must be animated again flipped? sorry, bad english :(

avatar image jagalibut · Jun 24, 2020 at 04:06 PM 0
Share

Hi, I know this is an old thread, but hoping someone can help me find a way to apply animation between the flipping. I want my character to perform a 'turn around' animation before doing a run animation as it flips. Thank you..

11 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by xpcrepair23 · Aug 21, 2020 at 01:34 AM

VECTOR 3 Lscale = transform.localScale; If (move > 0){ Lscale.x = 1; }

If (move < 0){ Lscale.x = -1; } Transform.localScale = Lscale; @bpioske

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by gustavopuebla95 · Oct 20, 2020 at 03:02 AM

void Update() { float mov_x = Input.GetAxis("Horizontal"); float Jump_y = Input.GetAxis("Vertical");

     Vector2 movForce = new Vector2(mov_x, 0) * speed ;

     rb2d.AddForce(movForce*Time.deltaTime);

     if (mov_x < 0)
     {
         sprite.flipX = true;
         
     }
     else if(mov_x > 0)
     {
         sprite.flipX = false;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by yuvaansh · Oct 20, 2020 at 03:23 AM

@bpioske Maybe this would help.

     private SpriteRenderer spriteRenderer;

     void Start()
     {
             spriteRenderer = GetComponent<SpriteRenderer>();
             FlipSprite();
     }

     void FlipSprite()
     { 
             if (move < 0) 
             {
                      spriteRenderer.flipX = true;
                      //This is based that your sprite is facing which way 
            }
            else if (move > 0)
            {  
                    spriteRenderer.flipX = false;
                    //Hope this would help
             }
       }










Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tubelightboy · Oct 28, 2020 at 12:03 AM

I tried setting the bool variable flip.X and flip.Y but the problem is that the colliders would not flip along with the sprites. This problem would make sense when you use a collider instead of just wrapping it around the character and for something else like wall detection (on collision) in the case of enemy patrolling or when no ground is detected (collision exit case).

if you want to flip the sprite along with the collider then this one worked for me:

 transform.localScale = new Vector2(-transform.localScale.x,transform.localScale.y);

here, you just flip the scale of the sprite on the x-axis and keep the y-axis as it is. Hope this would help someone.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ahand10 · Mar 14 at 06:10 AM

Such a valuable and helpful content that’s what I have been looking for a long time finally I have got it so really thanks for sharing such type of information. das wort wurde fleisch bild

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • ‹
  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animator - Stoping && Playing from Specific frames. 5 Answers

Changing "Sprite Mode" from Single to Multiple breaks reference to sprite sheet. 1 Answer

Sprite Renderer Missing Material after running 1 Answer

[JavaScript] Making an image sprite change upon button click? 1 Answer

What can I do to further optimize mobile game? (2D) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges