- Home /
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
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
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
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:
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
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.
I appreciate the help in the right direction but it would be even better if the answer was complete. What is 'initialOffset' ?
The value that was initially used to offset the euler angles