Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
32

Answer by JoeStrout · Apr 08, 2016 at 04:15 PM

This is an old question, but it comes up frequently in a Google search, so I thought I'd point out the new answer:

As of Unity 5.3, there is built-in "Flip" support. Just check whether you want to flip by X, Y, or both.

SpriteRenderer inspector

In code, you would just assign true/false to SpriteRenderer.flipX and .flipY.

Comment
Add comment · Show 4 · 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 msbalfe · Apr 10, 2016 at 05:02 PM 1
Share

This is super helpful! Thank you! I have been flipping using: theScale.x *= -1; transform.localScale = theScale;

But using the Sprite Rendered makes flipping trivial.

JoeStourt, thank you for sharing!

avatar image Master-Frog · Apr 21, 2016 at 10:09 PM 0
Share

Thanks, Joe. That reverse scale business came with problems if I tried to parent anything to the GameObject that I didn't want to scale.

avatar image kenncann · Jul 06, 2016 at 10:38 PM 0
Share

This was extremely helpful! I searched for like an hour because the flip function from the 2d overview tutorial wasn't working for me. Thank you so much!

avatar image Ghostling225 · Aug 03, 2020 at 06:18 PM 0
Share

Thanks, JoeStrout. I'm just starting out in Unity gamedev and this was super-helpful. Still works in 2020.1 BTW :D

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.

avatar image
1

Answer by trapazza · Jul 25, 2017 at 04:41 PM

I don't think modifying the transform is right. Keep both visual logic and game logic as separated as possible at all times.

Just ask the sprite renderer to do this and don't mess with the scale, it will probably backfire

Comment
Add comment · Show 1 · 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 tanoshimi · Jul 25, 2017 at 04:59 PM 1
Share

Bear in $$anonymous$$d that the sprite renderer wasn't available 2.5 years ago when this question was asked. Also, although your argument of separating visual logic and game logic is mostly correct, flipping the renderer only flips the sprite graphic - on almost every occasion you'll also want to flip the corresponding collider as well, which you achieve by mirroring the transform.

avatar image
1

Answer by muftiistikhori · Apr 17, 2018 at 09:50 AM

just set -1 in the scale x of the sprite of image UI if you want to get "mirrored image UI" without create new one in photoshop. hope this helpful..

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 JKXM · Apr 10, 2016 at 05:54 PM

This is quick but another way to flip is changing the local x scale to -1

 if (move < 0)
          {
              m_FacingRight = false;
              transform.localxscale  = -1;
 }
 


This should work i believe i have used this method before .

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.

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

Prevent 2D mask to impact other gameobjects ? 3 Answers

Finding the centre of two touches 2 Answers

Custom shader of sprite results in black alpha. 0 Answers

How do you check to see if an animation is playing in a Sub-State machine? 0 Answers

Resizing orthographic camera to fit 2d sprite on screen 1 Answer


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