- Home /
[2D] Flip (Mirror flip) sprite the right way
Well i've got a problem with my 2D character... The game is sidescroller so i need to flip the character. When i use method for changing the x size to -1, or rotating y for 180, i have my 2D Box collider not working right, it just messes up, my character falls through floor when it's on edge, it doesen't detect collision right (example: It thinks i'm touching another collider while i'm a lot away from it), and all other kinds of bugs, BUT ONLY WHEN it's rotated for 180, when it's in it's natural rotation, it's acting normally. ALSO - the method of attaching the sprite as a child to empty object and adding collider and rigidbody to the empty object instead IS NOT MY SOLUTION... My code is already big and it's connected and coded for the sprite, it's not universal...
I'd really like to have a solution for this or i'm gonna spend a whole week trying to make my freaking code work and connect to the sprite the other way, while i really don't have time.
Try creating a mirrored version of your sprite, and then switching the sprites material to the mirrored one whenever you need to. This won't mess up scaling, rotation or colliders. Assu$$anonymous$$g you created your sprite in Photoshop, to do this you'll want to: -Open a copy of your sprite in Photoshop. - Select the layer the sprite is on - Go to Edit -> Transform -> Flip horizontal. - Save the sprite's copy and import it into Unity. - then just change the sprite to the mirrored one whenever you need to via script, and vice versa. Hope this helps!
Thanks man but i cannot mirror flip it becouse the sprite is not just a regular sprite, it has it's animations and all the shit it cannot be just flipped like that...
Answer by patosalas · Jun 12, 2017 at 08:46 AM
I had the same problem and I solved it flipping the sprites in the spriteRenderer instead of scaling down in the x axis.
you have to access the SpriteRenderer via script and and set SpriteRenderer.FlipX to false or true depending on what you need
There is one major issue with this. If you're attaching other game objects to your sprite or have collision geometry that is based on the sprite - using the spriterenderer flip WILL NOT cover this. For example, if you're building a shooter and attach particle emitters for your engines or where you are going to shoot projectiles from - perfor$$anonymous$$g the flip in the spriterenderer will ONLY cause the sprite to change direction. All of the other objects parented to this GameObject will be facing the wrong way.
Answer by Spinnernicholas · Sep 30, 2014 at 09:16 PM
Whatever you do to mirror the sprite, also apply it to the collider.
You can do this easily by putting both the sprite and the collider under the same parent object and then mirroring the parent object.
This will keep everything lined up.
I think i tryed that, i will try again and i'll let you know if it worked
Answer by Free Arm Studios · Oct 22, 2015 at 08:16 AM
If you want to "Mirror" a sprite in a 2D game, figure out the appropriate axis:
If you want to flip vertically, switch the X rotation to 180. If you want to flip horizontally, switch the Y rotation to 180. If you want to rotate the sprite, switch the Z rotation. (These will only work appropriately if the sprite's pivot point is set to center).
Then, use this code:
transform.localRotation = Quaternion.Euler(xRotation, yRotation, zRotation);
(Transform referring to the object the script is attached to, localRotation referring to the rotation you want to change, and Quaternion.Euler is the rotation state in the 3D coordinate system)
Do not switch X or Y to anything other than 180, as it will produce undesirable results, since Quaternion.Euler is also used for 3D, but accepted for use in 2D. Think of your sprite as a plane. If it's sideways (90 degrees) on the X or Y, you won't see anything because the flat edge will be pointing at you.
Your answer
Follow this Question
Related Questions
Sprite flip movement script not working! HELP! 0 Answers
How to do a sprite flip 1 Answer
Flipping a 2D Sprite in Javascript 0 Answers
My sprite is teleporting when I change my scale to -1 to flip it 1 Answer
2D spaceship rotation 0 Answers