Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Flipping a 2D Character that has physics objects attached to it

Discussion in '2D' started by PixelSquad, Nov 20, 2017.

  1. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Hi guys

    I'm prototyping a little game that has 2D characters with ragdoll physics as well as animations.

    When I'd like to flip the character horizontally, animations work totally fine if I just scale the model by -1 in the Y axis. However physics breaks.

    What's the correct / best way of flipping such character horizontally?

    Just to give you an idea, I'm attaching a screenshot of the character. We're using: 2D Colliders, Rigid Bodies, and 2D Hinge joints.

    Many thanks!
     

    Attached Files:

  2. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    You can just use Transform.rotation instead of Transform.scale.
    If you add 180 or subtract 180 degrees in the inspector on one of the axis it should flip your character.

    Edit: If your character is a sprite you can also use the "flip" option of the Renderer.
     
  3. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Thanks Kaart. That was a good idea, but I've tried it and it also breaks physics. It looks like there's a conflict with the rigid bodies and joints when the character is rotated on the Y axis by 180 degrees. Collisions seem to work well at first sight.

    Perhaps if we were using the 3D physics engine this wouldn't have been the case.

    (Just correcting what I said above - the scale was on the X axis, so the rotation was on the Y axis)

    Any other ideas would be appreciated.
     
  4. Samacvuk

    Samacvuk

    Joined:
    Jan 8, 2017
    Posts:
    30
  5. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    So, I can't scale, and can't rotate. I can't flip the sprite because it's a character composed of many sprites: leg, torso, etc, and we will have animation clips playing on these characters.

    I thought about using the 3D physics engine (just to allow for rotation around the Y axis) but that would prevent us from using the stuff that's only in the 2D physics engine.

    Any ideas would be appreciated...
     
    Last edited: Nov 28, 2017
    Ali_V_Quest likes this.
  6. Samacvuk

    Samacvuk

    Joined:
    Jan 8, 2017
    Posts:
    30
  7. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Not sure I understand what you mean Samacvuk. What's "myMove" in your script?

    Also, about your link, I'm not trying to rotate the 2D character, but literally "flip" it so the same animations and physics setup can be used when it's facing left or right...
     
    Ali_V_Quest likes this.
  8. Samacvuk

    Samacvuk

    Joined:
    Jan 8, 2017
    Posts:
    30
    myMove is just a generic name for a variable representing the direction you want to rotate and Vector3.up is the default value (you don't need to type it)
     
  9. Ali_V_Quest

    Ali_V_Quest

    Joined:
    Aug 2, 2015
    Posts:
    138
    did you manage to solve this ?
    Thanks
     
  10. Jabberwoky

    Jabberwoky

    Joined:
    Apr 6, 2020
    Posts:
    5
    Took a whole day but i found a solution !
    1) Storage all Rigidbody Rotations and the JointAngleLimits2D min/max OnAwake.
    2) Calculate the angleoffset for all the connected Rigidboody (attachedRigidbody.rotation - connectedBody.rotation).
    !!you need to use the valuse that you will receive OnAwake!!.
    3) Flip the Objekt.
    4)
    JointAngleLimits2D temp = joint2D.limits;
    temp.min = limit.min * -1 + angleoffset * 2;
    temp.max = limit.max * -1 + angleoffset * 2;
    joint2D.limits = temp;

    on flip back just pass the defoult valuse-

    works for me,hope it helps,
     
    Volshar likes this.
  11. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,511
    If you are using normal map for 2drenderer, scaling negatively on x axis also breaks rendering.
     
  12. Jabberwoky

    Jabberwoky

    Joined:
    Apr 6, 2020
    Posts:
    5
    In Built-in RenderPipeline you can setup the shader to deal with the negativscaling,i guess."no idea on URP and HDRP". Negativscaling is the easiest way to flip but curses a lot of trouble.I simply find no easi and good way to flip my carackter, thet works for all scenarios.
     
    Volshar likes this.
  13. boypipz321

    boypipz321

    Joined:
    Jan 27, 2018
    Posts:
    7
    Good Day If there're still people struggling to this,

    I fixed mine by flipping the parent of all Physics objects you can see below, it might not be smooth but it does the job for me.
    Code (CSharp):
    1. public Transform Physics_Parent;
    2. public Transform Holder;
    3.  
    4.  
    5. public Transform Animation;
    6. public Transform real;
    7.  
    8. public void flip()
    9. {
    10.        
    11.  
    12.      
    13.         Holder.transform.parent = real;
    14.         Physics_Parent.transform.parent = Holder;
    15.  
    16.         Vector3 theScale1 = Animation.localScale;
    17.         theScale1.x *= -1;
    18.         Animation.localScale = theScale1;
    19.  
    20.         Vector3 theScale = Holder.localScale;
    21.         theScale.x *= -1;
    22.         Holder.localScale = theScale;
    23.  
    24.         Physics_Parent.transform.parent = real;
    25.         Holder.transform.parent = null;
    26.  
    27.        
    28.  
    29.  
    30.  
    31.  
    32. }