- Home /
How to flip sprite horizontally in Unity 2D?
I posted a question a few days ago, but haven't received any responses. I thought I would rephrase my question in hopes of getting some better feedback.
I'm trying to understand my options for flipping sprites horizontally in Unity 2D (specifically version 5.0+). From what I know, there are two main options:
Create sprites for facing right and facing left and switch between them as needed
Create a single sprite for facing one direction and multiply the transform's localScale by -1 whenever you want to mirror the image
I was using option #2 in Unity 4.X without any problems, but when I upgraded my project to unity 5.0, modifying the localScale made my sprite disappear. Any ideas why that might be the case? Please see the code in question below. If anyone has some efficient alternatives, please let me know.
// Flip sprite / animation over the x-axis
protected void Flip()
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
// Flip collider over the x-axis
center.x = -center.x;
}
And the animations that are in the sprite, or in the objetc that contains the sprite in the hierarchy, must be animated again flipped? sorry, bad english :(
Hi, I know this is an old thread, but hoping someone can help me find a way to apply animation between the flipping. I want my character to perform a 'turn around' animation before doing a run animation as it flips. Thank you..
Answer by JoeStrout · Apr 08, 2016 at 04:15 PM
This is an old question, but it comes up frequently in a Google search, so I thought I'd point out the new answer:
As of Unity 5.3, there is built-in "Flip" support. Just check whether you want to flip by X, Y, or both.
In code, you would just assign true/false to SpriteRenderer.flipX and .flipY.
This is super helpful! Thank you! I have been flipping using: theScale.x *= -1; transform.localScale = theScale;
But using the Sprite Rendered makes flipping trivial.
JoeStourt, thank you for sharing!
Thanks, Joe. That reverse scale business came with problems if I tried to parent anything to the GameObject that I didn't want to scale.
This was extremely helpful! I searched for like an hour because the flip function from the 2d overview tutorial wasn't working for me. Thank you so much!
Thanks, JoeStrout. I'm just starting out in Unity gamedev and this was super-helpful. Still works in 2020.1 BTW :D
Answer by acorrow · Apr 22, 2015 at 07:52 PM
I think the "proper" method here would be to use localRotation (assuming 2D here right?) and a Quaternion and flip back and forth as needed.... Just my quick example but I'm sure you could see how to adapt it. I still don't fully understand Quaternion's haha! But this should do the trick for you:
if (move < 0)
{
m_FacingRight = false;
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
else
{
m_FacingRight = true;
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
Hope that helps!
Adam
Use new solution by JoeStrout, because this solution made problems In my collision detection. I had tiles with Platformer Effector. I wanted to remove friction from platform sides (by unchecking "Use Side Friction") so player would fall down and not stick to the wall but because of the player's rotation it some how messed up that tile effector's behaviour - side friction was still working for right side of the wall.
This solved my problem when the player is parented to a moving platform. I was using transform.localScale which caused position and stretching issues on the sprite.
Answer by trapazza · Jul 25, 2017 at 04:41 PM
I don't think modifying the transform is right. Keep both visual logic and game logic as separated as possible at all times.
Just ask the sprite renderer to do this and don't mess with the scale, it will probably backfire
Bear in $$anonymous$$d that the sprite renderer wasn't available 2.5 years ago when this question was asked. Also, although your argument of separating visual logic and game logic is mostly correct, flipping the renderer only flips the sprite graphic - on almost every occasion you'll also want to flip the corresponding collider as well, which you achieve by mirroring the transform.
Answer by muftiistikhori · Apr 17, 2018 at 09:50 AM
just set -1 in the scale x of the sprite of image UI if you want to get "mirrored image UI" without create new one in photoshop. hope this helpful..
Answer by JKXM · Apr 10, 2016 at 05:54 PM
This is quick but another way to flip is changing the local x scale to -1
if (move < 0)
{
m_FacingRight = false;
transform.localxscale = -1;
}
This should work i believe i have used this method before .
Your answer
![](https://koobas.hobune.stream/wayback/20220613193759im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Prevent 2D mask to impact other gameobjects ? 3 Answers
Finding the centre of two touches 2 Answers
Custom shader of sprite results in black alpha. 0 Answers
How do you check to see if an animation is playing in a Sub-State machine? 0 Answers
Resizing orthographic camera to fit 2d sprite on screen 1 Answer