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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2d wheels sink into ground because the circle colliders change size on flip?

Discussion in '2D' started by paulxthompson, Aug 19, 2015.

  1. paulxthompson

    paulxthompson

    Joined:
    Aug 17, 2015
    Posts:
    6
    Hi All,
    New to this, and have spent a few days getting something i'm pretty pleased with when it's moving right: when my wheeled vehicle moves left it sinks into the surface below.

    I've done a few searches and know that there have been problems in the past with colliders stopping working when you flip a sprite to ( theScale.x *= -1 ) in 2D mode.

    A few quick pauses reveals that although the Circle Collider seems happy enough according to the numbers in the inspector, visually the circle representing it shrinks to a random size whenever I scale to -1. It returns to the correct size when I scale back to +1 and my vehicle moves on just fine.

    I've uploaded a screenshot (the scale happens in the parent object, and note that the wheels are actually sitting in the right place for the ground collider - it just doesn't happen to fit the graphic yet) - there's a lot of pale green in it but hopefully you can make out what i'm getting at.

    Thanks PaulxT
     

    Attached Files:

    Last edited: Aug 19, 2015
  2. paulxthompson

    paulxthompson

    Joined:
    Aug 17, 2015
    Posts:
    6
    Question to self - does the resize relate to the rotation? [I'll check later]

    Further info - the flip function looks like this at the moment - the problem exists without the foreach loops at the bottom - I was trying those based on some advice found nearby these parts, but which was dated from previous versions.

    voidflip()
    {
    //theSkull.MovePosition (Vector2.up * 300);
    theSkull.AddForce (Vector2.up * 200);
    //Switchthewaytheplayerislabelledasfacing
    facingRight = facingRight*-1;
    //Multiplytheplayer'sxlocalscaleby -1
    Vector3theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
    //forceallchildcolliderstoupdatetheirpositionbyenabling/disablingthem

    foreach(Collider2DcollideringameObject.GetComponents<Collider2D>()) {
    collider.enabled=false;
    collider.enabled=true;
    Debug.Log (collider.name);
    }
    foreach(Collider2DcollideringameObject.GetComponentsInChildren<Collider2D>()) {
    collider.enabled=false;
    collider.enabled=true;
    Debug.Log (collider.name);
    }
    }
     
    Last edited: Aug 19, 2015
  3. paulxthompson

    paulxthompson

    Joined:
    Aug 17, 2015
    Posts:
    6
    Ok - i've no idea why the wheel colliders were resizing, so I took a different approach and rotated 180 on the y axis. This left the wheels where they were, and they were stuck where they were because of the hinges they were attached to had anchor co-ordinates that was forcing them to be in a particular place? Bit confusing to me, and took a bit of thinking, and now my flip code looks like this:

    theHinges = GetComponents<HingeJoint2D>();

    void flip()
    {
    facingRight = facingRight*-1;
    for(inti = 0; i < theHinges.Length; i++)
    {
    Vector2hingeLoc = theHinges.anchor;
    hingeLoc.x=-hingeLoc.x;
    theHinges.anchor=hingeLoc;
    }
    if (facingRight<0)
    {
    transform.localRotation = Quaternion.Euler(0, 180f, 0);
    }
    else
    {
    transform.localRotation = Quaternion.Euler(0, 0, 0);
    }
    }

    Which works just fine - hope this helps someone at some point down the line :)
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Just to check, which version of Unity are you using? Also, if you could provide a small project that reproduces this issue, could you share it?