Search Unity

2D rotated collisions corrupted when parent's X is scaled -1 to flip

Discussion in '2D' started by Dreamwriter, Aug 15, 2014.

  1. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    I've got a baffling issue - I have a flying enemy in my game with a child BoxCollider2D sticking out, rotated on the Z axis at an angle as if looking with a search beam. It works great! Until I flip the enemy by scaling the parent's X to -1. For some reason that makes the BoxCollider2D a lot smaller, and its rotation doesn't change (though its position does). It's like it's rotating on the wrong axis somehow. If I cheat and flip the enemy by instead rotating the parent 180 degrees on the Y axis (as if it were 3D), that *looks* correct in the editor, but then the BoxCollider2D doesn't register collisions (which makes sense, it's not designed for 3D rotations).

    Any ideas on how I can fix this issue?


    rotatedcol.jpg rotatedcol_scaled.jpg
     
  2. cbothra

    cbothra

    Joined:
    Mar 14, 2014
    Posts:
    125
    Hi, Instead of having scaleX to -1, try to achieve the same using Transform.rotation which will rotate the colliders also.
     
  3. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    I tried that, but since the colliders are 2D, rotating them in 3D space makes them not function, though they appear correct in the Unity editor.
     
  4. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    The way that collider rotated really looks like a familiar bug in 4.3. If that's correct and you can't upgrade, one thing that works well is to make the collider in the same GameObject as the -1 x-scale.
     
  5. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    I'm using 4.5.3, and doing Trigger collisions. Unfortunately, I can't make the collider in the same GameObject as the -1 x-scale because then I'd have to rotate that object, plus that object needs its own box collider for detecting being shot or running into a wall.
     
  6. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    I really need to get this working tomorrow, so I guess I'm gonna have to go with a really crappy hack: have an empty parent object that does nothing and give it two children: a child that is the visual thing that controls motion and needs to flip (it'll have to change parent.position when it wants to move), and a child that is the scanner with its rotated collision (and have the visual child tell it to rotate to face the other direction when flipping).

    What sucks is, this is the kind of thing that will happen to TONS of 2D games, because it's rather common to need to flip a character in a 2D sidescrolling game.
     
  7. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    ...You do realize it's a fixed bug, you're just using an old version, right?
     
  8. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    No, as I responded when you asked about it, I'm using Unity 4.5.3, which is the current version available to download. There is a patch for 4.5.3, but no fix for this is mentioned in the patch notes. And I found another post about a similar issue, that stated that this has not been fixed, apparently they added a fix for colliders but not triggers.
     
  9. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Triggers ARE colliders, and lots of people do this routinely, so there must be something else going on.
     
  10. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    Here is the thread I was talking about:
    http://forum.unity3d.com/threads/unity-4-5-2d-colliders-rotation-change.248890/