Search Unity

Rotating sprite does not rotate collider

Discussion in '2D' started by DigiScot, Nov 20, 2013.

  1. DigiScot

    DigiScot

    Joined:
    Aug 23, 2013
    Posts:
    27
    When you rotate a sprite in either the X or Y axis, it does not rotate the collider, which is a killer if you try flipping to save on sprite graphics.

    The weird thing is if you go into collider edit mode (shift), the points are in the right place, but the actual collider is not... it's very weird

    Is this normal or a bug that'll get fixed?

    Would like to know so i can plan ahead with my graphics...
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It does though; I just tried it.

    --Eric
     
  3. DigiScot

    DigiScot

    Joined:
    Aug 23, 2013
    Posts:
    27
    What are you doing to test this? take a normal rectangle sprite, rotate it on the Z axis say 45o, then rotate it on the Y 180o, does everything look as it should to you?

    I get this: $CollIssue.png
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Ah, I was using a polygon collider. A box collider doesn't seem to work.

    --Eric
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It works as long as you only rotate along the z axis - which is all you should be using in a 2D game.

    If you need to flip the sprite, flip the signs for the localScale and z rotation.
     
    Last edited: Nov 20, 2013
  6. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    Looks like a bug. I've noticed this too but replacing it with a new one is a workaround or a using a polygon collider if it's a moving part.

    [EDIT]

    Yea the Z axis should suffice.
     
    Last edited: Nov 20, 2013
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That breaks batching though; rotating does not. It seems like a bug, since polygon and edge colliders work with arbitrary rotation, and I thought I read that box colliders in Box2D are implemented with polygon colliders under the hood.

    --Eric
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Rotating 2D colliders in the X/Y axes is not supported. By this I mean that all 2D colliders ignore transform X/Y rotation and only use Z. This is especially important when you consider that we're limited in what deformation we can achieve via Box2D for some collider types such as the Circle collider. Allowing this kind of arbitrary transform would mean Box2D would need to support ellipse (even sheered ones) which it doesn't support.

    This only parts of the transform component that are used are:

    X/Y position (Z ignored)
    Z rotation (X/Y ignored)
    X/Y scale (Z ignored)

    The colliders are not part of the SpriteRenderer component so whatever it does or doesn't use isn't tied into any of the colliders.
     
  9. Loxli

    Loxli

    Joined:
    Mar 11, 2013
    Posts:
    5
    So... If I need to rotate my arrow with the LookAt() function, how can I rotate the collider with the mesh?
     
  10. Yukichu

    Yukichu

    Joined:
    Apr 2, 2013
    Posts:
    420
    Related to my other post... a GameObject with Rigidbody2D + Interpolation on cannot rotate the X/Y axis at all. How do I rotate that? It absolutely will not rotate the GameObject. I don't want, nor am I trying, to rotate the collider/rigidbody, just the GameObject.

    I made a workaround of putting the rigidbody2D/collider2D in a different object than what I am rotating (sprite, animation, etc.) but... wow, threw me for a loop for like a week.
     
  11. AllanChan

    AllanChan

    Joined:
    Sep 2, 2013
    Posts:
    1
    You just need to set the object rigibody->fixed angle is true
     
  12. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    i also tried moving 2d animation collider not moving .this is a big problem
     
  13. AdriaandeHaan

    AdriaandeHaan

    Joined:
    May 10, 2014
    Posts:
    1
    I am also having trouble changing the Position of the Circle Collider 2D, it moves in the X position but changing the Y position seems to have no affect. Can anybody confirm this please? Just set the Scene View to 2D, add a Circle Collider 2D to your GameObject and try changing the Y position.
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It works fine here.

    --Eric
     
  15. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    its possible to make any video tutorial regarding 2d collider to move when animation play.its really helpful for u
     
  16. robzilla2000

    robzilla2000

    Joined:
    Aug 19, 2013
    Posts:
    4
    I don't understand the comment of "flip the signs of the z rotation and localScale".... I mean, I'm obviously missing something but why would that do anything at all? When looking at the character before it is flipped the Z rotation is set to zero. The Scale is set to 1. Setting the z scale to -1 has no effect on the sprite displayed... and obviously 0 * -1 is still zero. So the result is an unflipped image, and it doesn't solve the problem at all. ??? what am I missing ?
     
  17. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    It's not the Z scale. It's the X scale to flip horizontally, Y scale to flip vertically. Once you flip it (either way), any rotations you apply (which should be in Z) must also be sign-flipped, although at 0 rotation that's irrelevant (0 * -1 = 0).

    As long as you flip and rotate the same transform, you won't encounter any collider bugs, but if you flip a transform and then rotate a child Game Object, that child's colliders will not rotate correctly (in the version current at this time, reportedly already fixed for the next version).
     
  18. Senjor

    Senjor

    Joined:
    Aug 19, 2014
    Posts:
    1
    Using "-Scale" to Flip, it will be OK.
     
  19. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    It seems that rotating a Collider2D on Y, works in the inspector, but when I try to do it by code, it rotates visually in the inspector but stays physically in the same place. Is this due to the fact the rotation is not supported on Y for 2D colliders?

    EDIT:
    Actually I managed to fix this problem by adding a ridigbody to the object containing the collider. It seems to update the collider to the correct position.
     
    Last edited: Nov 27, 2014
  20. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    Same thing as Mish. Primitive 2D colliders seems to work well when paired with a rigidbody2d, I mean that you can rotate by X and Y and the collider also rotates, BUT ONLY IN INSPECTOR. When I change those X and Y rotation in script colliders don't do the job, they appear bad placed. Any help will be appreciated.
     
    Last edited: Dec 11, 2014
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    I cannot duplicate this. Can you please tell me what version you are using and how you set this up. Better still, please can you create a bug report and pass me the case number.

    Thanks.
     
  22. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    Before create a bug report I'll tell the version I'm in: 4.5.1f3 . I know that is not the latest, its close to the latest.
     
  23. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    I've been making a research and seems that this bug is fixed now. I'll show fixes related to this issue.

    4.5.2
    Physics 2D : Display a warning against a 2D collider when it is unable to create a Box2D collision shape due to it not meeting Box2D constraints.

    4.5.3
    Physics 2D: Fixed regression where 2D colliders didn't update when animated.

    4.6
    Fixed issue where 2D colliders break when used as prefab children.

    4.6.1
    Physics 2D: CircleCollider2D editing handles now show in the correct location when X/Y rotation is involved.
    Physics 2D: 2D physics colliders now correctly transform under full 3D rotation.

    Should work in latest version. I should update mmm :/ was afraid to do this due to fear of break something, I'll back up first. Meaby MelvMay can clarify if this issue is fixed now. Thanks.
     
  24. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    I've updated Unity and the issue still goes on. Even more, before(4.5.1) CircleCollider2D followed the transform when rotated in X and Y "angles"(in inspector), not now, it stay in place. That was the only change I see, before it behaved really rare when modified X and Y rotation by scripting, now at least it stay in place, still not what we want. I understand what MelvMay say, that X and Y rotation are not supported in Unity in 2D aspect in relation with CircleCollider2D because it should become an elipse more than a circle and doesn't exist, but I wonder why this rotation is not supported, because for example, see this case of use: I have leaves in my game, flat ones, and when I rotate X/Y in transform it looks great.

    What CircleCollider2D may do, for future releases, is that when you rotate X or/and Y in the transform, the collider just scale down. I don't know if this can get achieved, by now I'll try it in my game, meaby doing this colliders children of the leaves and making some hardcoding, since I need those colliders working well in my flat X/Y rotated leaves.

    Hope someone can give a light here. Thanks.
     
  25. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Ellipses are not supported in Box2D. That was a bug that was fixed where the gizmo showed it as an ellipse i.e. it was transformed by rotation. Behind the scenes during physics calculations only a circle was used and continues to be used since that fix.

    None of the other colliders are scaled-down due to rotation (they are rotated) therefore the circle doesn't either.
     
  26. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    Thanks for answer MelvMay, I'll attach a script which scale the ratio of my CircleCollider2D based on the X and Y rotation. Cheers.