Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to properly 'mirror' a complex object (e.g.: a character on x axis)

Discussion in '2D' started by AlanGameDev, Aug 19, 2014.

  1. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    Hello there.

    I'm having a hard time mirroring a 2d character. The game uses physics and to mirror the character I'm inverting the scale of the parent on x axis, unparenting everything, reverting the scale of the parent and reparenting everything again, it mostly works, although i have to explicitly set some things like joints anchors and stuff, however, some crazy things always happens and it's a pain to fix them, I have added a boolean so I check for it to do most of the math but some weird things always happens and I can't solve them.

    So, I'm wondering if there's another way to achieve the same result... it can't be that hard to simply 'mirror' a character in a 2d game.

    Any ideas?

    Thank you.
     
  2. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    Instead of scaling, you need to rotate the object 180 degrees around Y. To get this to render properly either:
    1. Make the object double sided, or;
    2. Use a shader that doesn't cull backfaces (I'm sure there are a load of these knocking around, as it seems like a pretty common requirement)
     
  3. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    Thank you for your reply.

    Rotating the object doesn't work because the rigidbodies (physics2d) won't work properly. The transforms won't rotate according to the physical objects.
     
  4. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    Ah, my mistake.

    Reviewing all the information out there, to do physics with the complexity you need the advice is apparently "stick to 3D". Hardly inspiring, but I suppose that's the only option and may make your life easier in the long run.
     
  5. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    Nah, 3d physics sucks for what I'm doing (a fighting game based on physics). Physx is simply not accurate enough, ccd tunnels even with high stepping rate and doesn't play nicely with rotating objects.

    I have a '2d entity flipper' script that is quite lengthy and still doesn't manage to flip the thing properly... it's quite depressing :).

    Perhaps Unity could introduce a feature where you could set some objects as 'flipped' so when they're turned around Unity will automatically cast some magic behind the scenes and make it work properly :D.

    Flipping stuff is a basic feature for 2d games imho.
     
    Mikael-H likes this.
  6. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    "And stuff" isn't a very good description of the problem. I've had issues with flipping and joint anchors - it seems the anchors do not get flipped. I just have two versions, enabling/disabling as necessary.
     
  7. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    "And stuff"... yeah... I don't have a better description cause I'm yet to find what causes most of the problems, I can say that animation doesn't work properly if the parent isn't flipped on x axis (so, flipping the child and preserving the parent scale is a no-go for animation). And also, I have some stuff that gets animated based on the positions of other stuff (like the 'upper' leg which is is rotated, positioned and scaled according to the calf). Sometimes the thing is obvious, like flipped stuff/rotated angles and it's pretty easy to workaround on code, but sometimes there's a subtle difference on the mirrored object that I simply can't track down.

    Besides that, Unity/box2d/whatever is full of bugs for 2D, for example, you have to deactivate and re-activate the objects before changing hinge joints limits otherwise they go crazy, plus alot of other things I don't remember right now, I hardly comment my code but recently it's full of comments like "workaround for a bug x" so I don't remove the lines even if they look dumb stuff :).
     
  8. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Hey Alan, I know it's a bit late in the day, but I'm looking for a solution for the same problem. Did you manage to find a good one?

    I did notice it looks like it's the hinge joints that go crazy when the 2D character is rotated by 180 degrees on the Y axis..... I'd appreciate it if you could comment on this.

    I also noticed that different versions of Unity seem to show different behaviour on how broken it looks. The latest version as of this writing (2017.2) is still broken but looks "a bit better" than 5.2 for instance.
     
    Last edited: Nov 28, 2017