• 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
1
Question by RickyX · Sep 30, 2014 at 02:43 PM · 2dspriteflip

[2D] Flip (Mirror flip) sprite the right way

Well i've got a problem with my 2D character... The game is sidescroller so i need to flip the character. When i use method for changing the x size to -1, or rotating y for 180, i have my 2D Box collider not working right, it just messes up, my character falls through floor when it's on edge, it doesen't detect collision right (example: It thinks i'm touching another collider while i'm a lot away from it), and all other kinds of bugs, BUT ONLY WHEN it's rotated for 180, when it's in it's natural rotation, it's acting normally. ALSO - the method of attaching the sprite as a child to empty object and adding collider and rigidbody to the empty object instead IS NOT MY SOLUTION... My code is already big and it's connected and coded for the sprite, it's not universal...

I'd really like to have a solution for this or i'm gonna spend a whole week trying to make my freaking code work and connect to the sprite the other way, while i really don't have time.

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 TheAlmightyPixel · Sep 30, 2014 at 07:37 PM 0
Share

Try creating a mirrored version of your sprite, and then switching the sprites material to the mirrored one whenever you need to. This won't mess up scaling, rotation or colliders. Assu$$anonymous$$g you created your sprite in Photoshop, to do this you'll want to: -Open a copy of your sprite in Photoshop. - Select the layer the sprite is on - Go to Edit -> Transform -> Flip horizontal. - Save the sprite's copy and import it into Unity. - then just change the sprite to the mirrored one whenever you need to via script, and vice versa. Hope this helps!

avatar image RickyX · Oct 01, 2014 at 06:57 PM 0
Share

Thanks man but i cannot mirror flip it becouse the sprite is not just a regular sprite, it has it's animations and all the shit it cannot be just flipped like that...

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Spinnernicholas · Sep 30, 2014 at 09:16 PM

Whatever you do to mirror the sprite, also apply it to the collider.

You can do this easily by putting both the sprite and the collider under the same parent object and then mirroring the parent object.

This will keep everything lined up.

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 RickyX · Oct 01, 2014 at 06:58 PM 0
Share

I think i tryed that, i will try again and i'll let you know if it worked

avatar image guilhermecorintho · Jan 31, 2018 at 11:29 AM 0
Share

Good idea!

avatar image
1

Answer by Free Arm Studios · Oct 22, 2015 at 08:16 AM

If you want to "Mirror" a sprite in a 2D game, figure out the appropriate axis:

If you want to flip vertically, switch the X rotation to 180. If you want to flip horizontally, switch the Y rotation to 180. If you want to rotate the sprite, switch the Z rotation. (These will only work appropriately if the sprite's pivot point is set to center).

Then, use this code:

transform.localRotation = Quaternion.Euler(xRotation, yRotation, zRotation);

(Transform referring to the object the script is attached to, localRotation referring to the rotation you want to change, and Quaternion.Euler is the rotation state in the 3D coordinate system)

Do not switch X or Y to anything other than 180, as it will produce undesirable results, since Quaternion.Euler is also used for 3D, but accepted for use in 2D. Think of your sprite as a plane. If it's sideways (90 degrees) on the X or Y, you won't see anything because the flat edge will be pointing at you.

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
2

Answer by patosalas · Jun 12, 2017 at 08:46 AM

I had the same problem and I solved it flipping the sprites in the spriteRenderer instead of scaling down in the x axis.

alt text

you have to access the SpriteRenderer via script and and set SpriteRenderer.FlipX to false or true depending on what you need

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 guilhermecorintho · Jan 28, 2018 at 04:59 PM 0
Share

Thank you so much for this!!

avatar image gregorypierce · Jan 31, 2018 at 03:54 AM 0
Share

There is one major issue with this. If you're attaching other game objects to your sprite or have collision geometry that is based on the sprite - using the spriterenderer flip WILL NOT cover this. For example, if you're building a shooter and attach particle emitters for your engines or where you are going to shoot projectiles from - perfor$$anonymous$$g the flip in the spriterenderer will ONLY cause the sprite to change direction. All of the other objects parented to this GameObject will be facing the wrong way.

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite flip movement script not working! HELP! 0 Answers

How to do a sprite flip 1 Answer

Flipping a 2D Sprite in Javascript 0 Answers

My sprite is teleporting when I change my scale to -1 to flip it 1 Answer

2D spaceship rotation 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