Search Unity

Collider dimensions erratic if attached to transforms with skew / non-uniform scaling

Discussion in 'Scripting' started by Zergling103, Jun 6, 2013.

  1. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    Hi there,

    I was wondering what math Unity's developers used to determine what shape, size, rotation and position a collider will have after being transformed by X transform.

    This is predictable - and likewise easy for me to figure out on my own - if the transform, and all of it's ancestor transforms have uniform scaling (x = y = z). This will usually be the case.

    But, it is very easy to break this predictability even with simple configurations. You can reproduce this bug on your own: Create a capsule collider, and change its direction field to X or Z. Now scale the transform upwards in the Y direction. You'll notice that the capsule becomes longer, not thicker, when doing this - almost as if the developers were assuming that capsules always had their direction set to Y.

    Try this with more complex configurations: For example, parent this capsule to a new game object, and then parent that game object to another. Scale the root along the X axis, then rotate the parent along the Y axis. Pretty trippy stuff.

    I hope nobody has designed a part of their game to rely on this weird collider transforming logic. Nonetheless, I was writing an extension function on the CapsuleCollider object which returns the height, position, direction and radius after being transformed. For what I need right now I could just assume uniform scaling, but ideally I want it to be robust enough to match exactly what Unity computes in all its weirdness. This raises another question - shouldn't Unity fix this weirdness in the first place?