Search Unity

Flipping Sprites + Colliders

Discussion in '2D' started by Brutang, Dec 11, 2013.

  1. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    I've been searching and experimenting to find a solution to this for a week now, hopefully there's an answer.

    When I flip my object using localScale.x = -1 the sprite looks fine but the colliders go crazy (multiple GameObjects/colliders for 1 enemy).



    The colliders also shrink/expand based on rotation (larger as rotation approaches 0, smaller as it approaches 360).

    My hierarchy is:

    Enemy [parent] <-- being the one I flip via localScale.x=-1
    -body
    -jaw
    -left_arm
    -right_arm
    -left_leg
    -right_leg
    -head

    I understand it's an issue with non-uniform scaling so I tried changing that to the following and flipping the CHILDREN instead of the parent:
    Enemy [parent]
    -jaw
    -left_arm
    -right_arm
    -left_leg
    -right_leg
    -head

    That still didn't work.

    I also tried:

    Enemy [parent]
    -jaw
    --jaw collider
    -left_arm
    --left_arm collider
    -right_arm
    --right_arm collider
    -left_leg
    --left_leg collider
    -right_leg
    --right_leg collider
    -head
    --head collider

    I tried this solution as well but just couldn't figure it out.

    Any help would be GREATLY appreciated! I've been stuck on this for a week.
     

    Attached Files:

    Last edited: Dec 11, 2013
    Rutenis likes this.
  2. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    That's odd. Could you submit a bug report?

    Also, rotationg around Y by 180 should work with Sprites as well.
     
  3. DigiScot

    DigiScot

    Joined:
    Aug 23, 2013
    Posts:
    27
    Upgrade to the latest version of Unity (4.3.1), this seemed to have fixed this issue.
     
  4. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    DigiScot: I'm on 4.3.1f1

    TomasJ: Rotating on the Y causes weird issues. The dinosaur is made up of several sprites arranged on the Z layer. Rotating Y to 180 makes his left leg in front of the right, etc. You can fix the look by inverting the Z position BUT the colliders don't move with the sprites:



    I read something the other day by someone on your team, I believe Eric, saying that you should only rotate sprites on the Z axis in a 2D environment. Obviously you can't "flip" a sprite by rotating it on the Z.
     
    Last edited: Dec 11, 2013
  5. DigiScot

    DigiScot

    Joined:
    Aug 23, 2013
    Posts:
    27
    Hi Brutang, turns out I was wrong (as you know) with the version, it wasn't that it started working for me, it's because I changed to using the polygon collider, which does rotate correctly. It's not ideal, but it's workable till they fix it, and you can easily delete points to get it down to a similar number or verts, tho I couldn't say how it compares in performance!
     
  6. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    The colliders are all Polygon Collider 2Ds; 6 of them. Rotating on the Y or flipping the parent via localScale.x=-1 both cause issues. Flipping via localScale causes non-uniform scaling issues (like shrinking/expanding depending on rotation) and rotating on the Y doesn't adjust the colliders (as shown in the image).

    I'd love to see how you do it via code or an image as I'm at a loss for how to fix this!
     
  7. Yukichu

    Yukichu

    Joined:
    Apr 2, 2013
    Posts:
    420
    I had collider issues when flipping things but I found I had to... kind of rotate things in a weird way.

    Pause the game and in the editor play with the rotational values to see if you find what works.

    I had to do a Quaternion.Euler(0, 90, 270) if the object was on the left and a Quaternion.Euler(0, 270, 90) if it was on the right to get the correct effects, depending on which direction something was instantiated then rotated via Quaternion.LookAt(x)

    Hopefully something like this helps.
     
  8. DigiScot

    DigiScot

    Joined:
    Aug 23, 2013
    Posts:
    27
    It's the Rigidbody that messes it up, if you don't have one, poly colliders rotates fine that's why it was ok in my tests I did. Really annoying this one!
     
  9. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    We reproduced this case and a fix will be available in the next release. Sorry for any inconvenience.
     
  10. DigiScot

    DigiScot

    Joined:
    Aug 23, 2013
    Posts:
    27
    Great news TomasJ!
     
  11. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    Thanks, TomasJ!

    Looking forward to it.
     
  12. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    doing a localscale = -1 really messes with the colliders. It would take a working collider and instantly mess up the detection once the scale was reversed, I had to resort to rotation on y
     
  13. spayced

    spayced

    Joined:
    Sep 14, 2013
    Posts:
    7
    I solved this by enabling and re-enabling the collider every time it's rotation changes using the second method you described. An ugly fix, but it does the job.
     
  14. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Any idea when the next release will be? I really need this fix :(
    Thank you
     
  15. TaewYn

    TaewYn

    Joined:
    Jan 30, 2013
    Posts:
    19
    Has this been fixed with 4.3.3 ?
    I still seem to have really weird things going on when rotating child objects of flipped sprites.
     
  16. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    I confirm that this problem has NOT been fixed in Unity 4.3.3 :evil:
     
  17. DigiScot

    DigiScot

    Joined:
    Aug 23, 2013
    Posts:
    27
    :( really sad news
     
  18. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    i had all these issues on my previous project and i had to abandon it , not only colliders, joints go nuts as well, subscribed to this thread i hope unity fix it soon
     
  19. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Yes I confirm that joints go nuts as well.

    @TomasJ
    Can you please give us an ETA on when all of this will get fixed?
    Thanks
     
  20. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    BUMP! :p

    @MelvMay can you please tell us if this will be fixed in the next release?

    Thanks
     
  21. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    I have the same problem.

    I'm changing the center of the collider when i rotate my sprite as a workaround, it's working by now but i know that is not the best way to do this.

    Waiting for the next release...
     
  22. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    Same here. Glad the devs are aware; been putting enemy development on hold for now :(
     
  23. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  24. herpanda

    herpanda

    Joined:
    Jan 1, 2014
    Posts:
    27
    Has the "next major release" released with the fix yet? I'm not sure what's considered a major release, and my internet is too slow to check myself if it hasn't been fixed. Thanks.
     
  25. TaewYn

    TaewYn

    Joined:
    Jan 30, 2013
    Posts:
    19
    Nope, no fix for this yet I'm afraid.
     
  26. herpanda

    herpanda

    Joined:
    Jan 1, 2014
    Posts:
    27
    Thanks for the update TaewTn
     
  27. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Any action this? It's brutal. My workaround: create a flipped copy of the sprite assets with a whole corresponding hierarchy of associated gameobjects. Instead of flipping the parent transform, I disable one branch and enable the other. Works great - of course - but what a pain.
     
  28. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    I am unfortunately having to do the same thing.
    Well Unity version 5.0 has been announced. Its just a matter of waiting for it to go live. Then apparently everything will be fixed :rolleyes:
     
  29. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    It's fixed in 4.5 which is coming out soon™
     
  30. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Hi Tomas, there is webpage which i can see the release plan!?
     
  31. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    When it's out, it will show up on the Download page. There's no public schedule of when updates are released. There's a "release notes" link on the Download page as well for seeing what changed in an update.
     
  32. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Well... at least I made my question. Who knows it really exist !?
    What is release I already read all the release notes. ;)
     
  33. AMO_Noot

    AMO_Noot

    Joined:
    Aug 14, 2012
    Posts:
    433
    Looking forward to this fix. I was trying to figure out why rotating a mirrored game object would screw up all of my colliders; it was driving me crazy. Hopefully soon™ means a week or two at most...
     
  34. DonPerry

    DonPerry

    Joined:
    Oct 4, 2012
    Posts:
    16
    I have the same problem. When is 4.5 due?

    $workingCollider.png $bustedCollider.png
     
    Last edited: Apr 28, 2014
  35. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    One thing that's helped me, is that if you flip the same transform you rotate, it still works, even for colliders of child GameObjects (as long as you don't in turn rotate them separately within the flipped object). Potentially, you can even flip a set of siblings and animate them together, though that's rather more work to animate, since you can't use their parentage and pivot to lock their positions together.
     
  36. DonPerry

    DonPerry

    Joined:
    Oct 4, 2012
    Posts:
    16
    Thanks for the reply. I'm now having a VERY similar problem with parenting a HingeJoint2D or any kind of joint for that matter.
     
  37. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    Happy to say as of today (6/24/14) on 4.5.1f3 the issue is fixed.

     
    Last edited: Jun 25, 2014
    Kurius likes this.
  38. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Glad to hear it @Brutang!
    Unfortunately the fix came too late for me. I had to find workarounds in order to properly flip the tractors in my game Tractor Crew.
    Anyway, good luck with your game! I can't wait to play it!
     
    Brutang likes this.
  39. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Where are you finding this release? I don't see anything in the announcements.
     
  40. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    It popped up in Unity when I opened the editor saying I was out of date. Should be the latest version here.
     
  41. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Are there any release notes for it anywhere? The last I see are for 4.5.1p2.
     
  42. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    Not that I see; I think I read something about the f2 f3 etc updates are patches, so they don't treat them as full version releases. Could be wrong, been cramming a lot into my brain today :)
     
  43. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    I rotate my player on Y... Works fine, the scale = -1 and 1 really messes unitys colliders up i found, but rotation worked fine, it rotated the colliders with the sprite....
     
  44. Vojta Koci

    Vojta Koci

    Joined:
    Nov 19, 2013
    Posts:
    64
    Yes, negative scales are real pain. Are there any plans to fix this problem?
     
  45. TimothyGates

    TimothyGates

    Joined:
    Jan 31, 2019
    Posts:
    19
    2019, and still colliders don't flip with player sprite
     
  46. cossacksman

    cossacksman

    Joined:
    Dec 2, 2017
    Posts:
    10
    Pretty sure they still aren't in 2021..
     
  47. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    There are no bug reports of this. Do you have a screenshot showing this?
     
  48. calvxrr

    calvxrr

    Joined:
    Jan 4, 2021
    Posts:
    1
    Hi.
    I confirm colliders don't flip with sprites. I send attachments.
    When I flip sprite, collider stays in the same position.

    Unity version 2020.3.5f1 flip-x.png no-flip.png
     
  49. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Yes, they're not supposed to. Colliders can be flipped with Transform scale which is what the thread is about. The whole point of the flip sprite feature is to avoid flipping everything else that relies on the Transform, including children. "Flipping" colliders means recreating them from scratch.
     
    ScottAdams likes this.
  50. witexon

    witexon

    Joined:
    Jul 14, 2022
    Posts:
    2
    Unfortunately, as of today, the character still does not turn, but only changes in size
     
    leegod and ScottAdams like this.