• 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
15

Answer by acorrow · Apr 22, 2015 at 07:52 PM

I think the "proper" method here would be to use localRotation (assuming 2D here right?) and a Quaternion and flip back and forth as needed.... Just my quick example but I'm sure you could see how to adapt it. I still don't fully understand Quaternion's haha! But this should do the trick for you:

         if (move < 0)
         {
             m_FacingRight = false;
             transform.localRotation = Quaternion.Euler(0, 180, 0);
         }
         else
         {
             m_FacingRight = true;
             transform.localRotation = Quaternion.Euler(0, 0, 0);
         }
 

Hope that helps!

Adam

Comment
Add comment · Show 2 · 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 Helminth · Jan 05, 2018 at 06:22 AM 0
Share

Use new solution by JoeStrout, because this solution made problems In my collision detection. I had tiles with Platformer Effector. I wanted to remove friction from platform sides (by unchecking "Use Side Friction") so player would fall down and not stick to the wall but because of the player's rotation it some how messed up that tile effector's behaviour - side friction was still working for right side of the wall.

avatar image MGEEZ · Mar 14 at 03:20 AM 0
Share

This solved my problem when the player is parented to a moving platform. I was using transform.localScale which caused position and stretching issues on the sprite.

  • ‹
  • 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