Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Dynamic RigidBody2D on child GameObject

Discussion in '2D' started by milanhenkrich, Nov 5, 2019.

  1. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    28
    Hey Guys,

    I m struggling with odd behaviour for RigidBody2D.

    I created game object (consisting only of Transform) "Parent".
    I added multiple children into "Parent". Into children I added polygon colliders and also rigidBody2D set to Dynamic.

    My problem is that it seems like RigidBody2D doubled PolygonColliders on Children.

    Screenshot 2019-11-05 at 19.43.14.png
    When I take Children out of "Parent" then everything is fine.

    Can you tell my what am I doing wrong?

    Thank you
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,134
    try to make a gameobject with the collider and rigidbody2d and then add it to the parent.
     
  3. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    28
    Thank you for reply...
    Actually this is the case. My child gameObject is prefab and it has PolygonCollider2D, RigidBody2D, Mesh filter and MeshRenderer. Before I instantiate it, I modify mesh and then assign points to PolygonCollider2D.

    The problem still persist.
     
  4. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    28
    I tried also to instantiate child, and then assign its transform.parent to transform of "Parent". But it didn't help.
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,134
    do you have deleted then the old points ?
    I think, the rigidbody2d can't change the collider. There is something wrong with the new points.

    And you can make your collider in the prefab, then disable it if the object is child and enable it if the object lost the parent.
     
  6. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    28
    I am not sure how to delete them.

    I just used "GetComponent<PolygonCollider2D>() . points = points". I think this will overwrite it.


    Screenshot 2019-11-05 at 21.38.19.png

    You can see on image that for one selected child :
    1. There is only one path for polygonCollider2D, and only one polygonCollider2D component on the object
    2. I can modify original path of polygonCollider, but for physics it is ignored.
    3. It is also displaying another polygonCollider2D, and I don't know where it come from - seems like any offsetted version? Physic is based on this collider, not on original.
     
    Last edited: Nov 5, 2019
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,134
  8. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    28
    This is very odd. I found where is problem. This unwanted behaviour occurs only when I set "Parent"'s scale x and y to be not equal. So when I set parent's transform scale x = 5, and transform scale y = 5, then everything is fine. But when I set it to x = 2 and y = 5, then this problem occurs. Thank you for you help
     
    vakabaka likes this.
  9. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    28
    I tested it in other project what I have and the same problem happens. I think there is bug in unity.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,004
    I tried to quickly duplicate what you described but couldn't.

    I would suggest any of the following:
    • Send in a bug report with a simple reproduction project and give me the case number
    • Describe here the Unity version and exactly how to duplicate the issue, step by step
    • Upload a reproduction project in my personal folder here also providing reproduction instructions
    Hope this helps.
     
    Last edited: Nov 9, 2019 at 2:11 PM
    vakabaka likes this.
  11. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    28
    Hello MelvMay,
    I just tested it again, on completely new project and I am getting the same behaviour. I uploaded it to you personal folder.

    I am using unity 2019.2.10f1.
    So in order to reproduce bug:

    1. Set parent scale x and y to be different values: let say x = 3, y = 1.
    2. Now when you look on child (it is frog in sample project) collider, everything is fine. It is same. But if you lift child, and let it drop on ground, and then when you look on collider it looks doubled.

    I am attaching picture, you can see here how collider is doubled.
    Screenshot 2019-11-10 at 15.30.44.png


    When you have look on this, please let me know whether it is bug or I am just doing something wrong.
     
    MelvMay and vakabaka like this.
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,004
    So first thing to note is that this is "just" a gizmo thing, there are not two colliders. The gizmo first draws the physics shapes (the sub-polygons) and you can see that count in the PolygonCollider2D Info roll-out in the inspector. It then draws the outline that you actually specified by editing. This outline is used to create those sub-shapes.

    Anyway, a couple of things are at play here that isn't too obvious. The first is that the outline gizmo is using the raw scaling when it shouldn't so this is the "double" collider you're seeing and this outline is incorrect so on that basis alone I would submit this as a bug report and send me the case number and I can get it fixed. It's the non-uniform scaling that's throwing it off.

    The second is nothing to do with physics and it's the first time I've noticed this but for the SpriteRenderer there seems to be a difference when you use non-uniform scaling on a parent as opposed the same scaling on the GO the SR is on. I've just asked the 2D team which this is the case. It does look like scaling is applied post-rotation in that case. This means that it won't be the same as the collider shapes (2D or 3D for instance). Collider shapes are static and are not dynamically scaled in the XY axis. They are scaled correctly when they're created and from then on they just move or rotate as the body does.

    So in short; it's not a double collider but an outline gizmo rendering issue specifically for the PolygonCollider2D.
     
  13. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    28
    Thank you MelvMay, okay, so it is just outline gizmo.

    Please note that physics is applied to this new outline "gizmo", and not to actual PolygonCollider2D anymore. See my first picture above.