- Home /
2D character flipping issue
Hello,
When I press a button my character flips the direction I want and all is well as long as I am using the scaling method(Scaling -1 on x axis). But now I want to rotate the character by 180 degrees on the y instead. When I do though the character will flash for a second in the right direction and then face left again and stay left.
So when I press the Move Right Key my character flips for an instant and then flips back to facing left. I can continue to walk right but He will face left.
Here is all the flipping code
void Update()
{
//Flip Direction
if(horizontal > 0 && !faceRight)
Flip ();
else if (horizontal < 0 && faceRight)
Flip ();
}
void Flip()
{
faceRight = !faceRight;
transform.eulerAngles = new Vector3(0, 180, 0);
return;
}
in the 2d character controller, this is how they did flip
void Flip(){
faceRight= !faceRight
Vector3 theScale=transform.localScale
theScale.x*=-1;
transform.localScale=theScale
}
Answer by Fubiou · Sep 07, 2015 at 07:33 AM
Hey, I found a simple way to flip the 2d character with a simple line:
transform.localEulerAngles = transform.eulerAngles + Vector3(0,180,-2*transform.eulerAngles.z);
Oh yeah that works
THAN$$anonymous$$ YOU SO $$anonymous$$UCH
Answer by Lazdude17 · Jun 02, 2014 at 09:56 PM
I have already done that but it doesn't work with my current set up because I'm using Sprites & Bones package to get the IK functionality. It messes up the bones if you scale so I need to rotate instead but It will not work for me.
Answer by JDelekto · Apr 28, 2019 at 10:51 AM
Assuming that you're trying to do a mirror image flip, have you considered taking the component of your scale property and multiplying it by -1?
So if I have a sprite which is facing right, I can use transform.localScale = new Vector3(transform.localScale.x, -transform.localScale.y, transform.localScale.z).
If you can choose to do a 180-degree flip on any axis and combine them as well.