• 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
2
Question by baguwka · Nov 22, 2013 at 10:17 PM · animation2drotationmovement

How to properly flip 2D character with new Unity4.3 2D framework? (Have problem with animation rotation)

Hello there, i'm already checked official Unity 4.3 example project, and there they use

 Vector3 theScale = transform.localScale;
 theScale.x *= -1;
 transform.localScale = theScale;

This works pretty well, but. But i'm encountered an trouble, not sure this trouble with this Flip or with something else.

I'm use Animator to animate my characters, i have Attack animation, which in addition to the sprite animation also move character's Hand, which can carry the weapon. Problem is when i flip the character to opposite direction hand will move at characters back, which is wrong. (video below will be more descriptive)

I discovered that this problem occure only if i modify the Hand's rotation in animation, so if i works only with .position this works well, but i need the rotation!

What is best way to get this work?

Here the youtube video: http://youtu.be/qpMK2gRgDz8

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 joshpsawyer · Mar 31, 2014 at 03:08 PM 0
Share

I've started a feature request to enable the 'mirror' flag in $$anonymous$$ecanim to be used on 2D animations -- if we can get it implemented, then we can avoid these workarounds in the future. http://feedback.unity3d.com/suggestions/2d-allow-mirror-flag-in-mecanim-to-be-used-with-sprite-animations

avatar image TiMMyyMMiT · Apr 01, 2014 at 08:29 AM 0
Share

Not a bad move. For the time being I would definitely recommend sticking with the 3D physics system until the improve the 2D one. If you wait for this fix you will be waiting forever

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by TiMMyyMMiT · Nov 25, 2013 at 11:37 PM

Flipping the X-scale will cause issues with child objects (especially colliders). The 3D solution is to rotate your character by 180 degrees along the y-axis which will fix your problems except that 2D colliders do not flip so they will animate incorrecly.

My current fix for the colliders is to set each collider as a child for the GameObject you want:

alt text

Then I manually set the rotation of the collider at each frame:

 void LateUpdate()
 {
     rotation = parent.rotation.eulerAngles;
     rotation.x = rotation.y = 0;
     if (facingRight)
     {
         rotation.z *= -1;
         myTransform.eulerAngles = -rotation + initialOffset;
     }
     else
         myTransform.eulerAngles = -rotation - initialOffset;
 }

Not my favourite solution but currently there is very little you can do as far as flipping complex characters in Unity's 2D right now. The image below shows this working.

By far the easiest approach would be to continue to use the 3D colliders and 3D physics and just rotate your character at 180 around the y-axis

alt text


colliders.png (8.2 kB)
i did it2.png (152.5 kB)
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 TiMMyyMMiT · Nov 26, 2013 at 01:14 AM 0
Share

It is also worth noting that none of this works with rigidbodies. If you plan on using complex colliders and something like ragdolls then just stick with 3D.

avatar image baguwka · Nov 26, 2013 at 02:05 AM 0
Share

Thank you for descriptive answer, this is pretty useful.

avatar image robzilla2000 · May 18, 2014 at 05:40 PM 0
Share

I appreciate the help in the right direction but it would be even better if the answer was complete. What is 'initialOffset' ?

avatar image IsaacP · Feb 03, 2015 at 02:01 PM 0
Share

The value that was initially used to offset the euler angles

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

21 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

Related Questions

Character still walking when no key is already pressed 1 Answer

Animation in Unity editor prevents rotation in script 1 Answer

how to make an object move towards it's rotation a set amount as a child in 2d 0 Answers

Animated sprites disappear then rotated on Android 6 Answers

Having Issues Rotating 2d Sprites to face another 2d Object 3 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