I have a rigged 2D character with Sprites And Bones in Unity and I use inverse kinematics to animate it.
But if I want to flip the X-axis, my character go berserk :
I have a script attached to "Karateka", containing a simple Flip()
function :
public void Flip(){
facingRight = !facingRight;
Vector3 karatekaScale = transform.localScale;
karatekaScale.x *= -1;
transform.localScale = karatekaScale;
}
I have the same kind of effect with an other IK script, Simple CCD, from Unite 2014 - 2D Best Practices In Unity, so I may just do something stupid.
What can I do to properly flip my character?
EDIT (for Mark) :
I got it to work using this :
public void Flip(){
facingRight = !facingRight;
/*
Vector3 karatekaScale = transform.localScale;
karatekaScale.x *= -1;
transform.localScale = karatekaScale;
*/
GameObject IK = GameObject.Find ("IK");
GameObject skeleton = GameObject.Find ("Skeleton");
Vector3 ikScale = IK.transform.localScale;
ikScale.x *= -1;
IK.transform.localScale = ikScale;
if (facingRight) {
skeleton.transform.localEulerAngles = new Vector3(skeleton.transform.localEulerAngles.x, 180.0f, skeleton.transform.localEulerAngles.z);
} else {
skeleton.transform.localEulerAngles = new Vector3(skeleton.transform.localEulerAngles.x, 0.0f, skeleton.transform.localEulerAngles.z);
}
}
Okay, so as we discussed in comments, just to sum it up:
1) You don't flip everything and/or don't reset bones correctly, that's why the animation "falls apart" on flipping
2) One can do a SpriteRenderer
.flip[X/Y] but this should be done on every element of the sprite
3) Skeleton script has an "Edit mode" checkbox. When it's checked and one flips the character using scale.x *= -1
, can see that everything except the bones are flipped
And last but not least, the question got updated with the code snippet providing the "code level solution" to the issue.
Thanks for sharing all the details Cyrille as well as I'm glad I could help.