Last active
July 25, 2025 00:26
-
-
Save hariedo/187c08c3fc3944df5778a6a932df9f9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// If the effective scale of a BoxCollider ends up making the .size negative, | |
// such as when a prefab is scaled (-1,+1,+1), then Unity complains loudly: | |
// | |
// /!\ BoxColliders does not support negative scale or size. | |
// | |
// This works around the problem by looking at the signs of the lossy scale, | |
// and flipping the same signs in the box collider's size vector. Since | |
// the box collider is always aligned with the local matrix, this has no | |
// effect on the collider except to quiet the console warnings. | |
// | |
public static void FixNegativeBoxCollider(BoxCollider box) | |
{ | |
Vector3 lossy = box.transform.lossyScale; | |
Vector3 flip = new Vector3( | |
Mathf.Sign(lossy.x), | |
Mathf.Sign(lossy.y), | |
Mathf.Sign(lossy.z)); | |
Vector3 sign = new Vector3( | |
Mathf.Sign(box.size.x), | |
Mathf.Sign(box.size.y), | |
Mathf.Sign(box.size.z)); | |
if (flip != sign) | |
{ | |
Undo.RecordObject(box, "Fix Negative Box Collider"); | |
box.size = Vector3.Scale(box.size, flip); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment