- Home /
Box colliders and negative scales
I'm trying to "flip" objects with a negative X scale. In 5.3.4 this works well. In 5.4 , box colliders seem to be actually working correctly, but somewhat ironically we get spammed with this warning for every instance of a flipped collider:
BoxColliders does not support negative scale or size. The effective box size has been forced positive and is likely to give unexpected collision geometry. If you absolutely need to use negative scaling you can use the convex MeshCollider.
Now I can imagine that certain configurations with negative scales do act badly with box colliders, but in our case of a simple (-k, k, k) type of scale they seem to work well enough for our purposes. Are there lurking issues even with this type of setup? Is there a way for us to disable this warning?
Flipping objects by applying a -1 scale to one axis is something that a lot of people want to do for certain types of games, so it would be handy if this was supported as cleanly as possible.
Answer by money4honey · Nov 04, 2016 at 08:44 PM
so, if you want use negative scale, you must replace box collider with convex MeshCollider.BoxColliders does not support negative scale or size. The effective box size has been forced positive and is likely to give unexpected collision geometry. If you absolutely need to use negative scaling you can use the convex MeshCollider
I don't have a mesh, it's a 2d game.
Can I use $$anonymous$$eshCollider with default Unity box model?
This seem wrong to me.
But it' always worked, why break compatibilty now?
I don't know why unity devs break this compability for now, maybe they do this due performance optimization. if you create 2D game, you can separate colliders and sprite renderer in different objects (which have same parent), and set negative scale only for sprite renderer. it works for me
Answer by xTyler73 · Jun 19, 2017 at 09:40 PM
A way to fix this is to actually set the scale of the object with the box collider that is causing the warning to be negative. The negative of this object combined with the parent object that is negative cancels them out to be positive.
Thank you! I had this in an asset from the store which uses scale (-1,1,1) for mirrored child 3D game objects. Simply changing the scale x to -1 in the child objects with the collider was a simple workaround!
Answer by omarmoh-om · Aug 15, 2017 at 09:12 PM
I had the same Issue and it isn't that difficult to fix too. Somewhere in your project where you are re-scaling or resizing a GameObject (With a box collider attached to it) and or a Box collider, and the variables that calculates that getting subtracted. Subtracting a positive from negative you now have the chance to make the size or scale negative; which mathematically wrong, like having the distance to be negative. for example, if you are doing something like this with a variable named foo:
foo.transform.localScale = new Vector3(foo.transform.localScale.x, foo.transform.localScale.y, foo.transform.localScale.z-1f);
this will generate the error because of the chance of the z-axis scale to be 0.5f, this causes the scale to be -0.5 which is now allowed or possible. It will cause weird collisions and detection from unity side if you are going with this. To fix this problem and avoid having the warning or negative values you must use:-
float newZScale = Mathf.Abs(foo.transform.localScale.z - 1f);
foo.transform.localScale = new Vector3(foo.transform.localScale.x, foo.transform.localScale.y, newZScale);
Mathf.Abs, take the whatever the value is and make it positive This way even if the values generated a negative value it will still get accepted and transformed into positive ones, and it will get rid of this warning in Unity 5.4.2f2.
Doesn't this only scale the body and children? The question talks about "flipping", can this be achieved?
Answer by eirikamb · Aug 11, 2016 at 09:23 AM
The way to work around the error is to hae all positive but change the centre... It dont seem like you want an answer but just a place to complain, but this is a way to work around it
In the box collider Settings... Right above size is center, that is how you change the position of the collider... If you change it negative to half of the size, it should be exactly like havig for example -1 in size
I think you've misunderstood my question, I'm assigning negative scale not exactly to boxCollider component but to transform component's scale
but you didnt actually have a question in your post, you didnt ask for a way to fix it, just complain of how it is not working like it used to before
We used to flip characters with applying negative scale, and that wasn't a problem for years and now suddenly Unity shows some warnings. And I don't know whether it's my fault or Unity's bug or something else.
Answer by trialforce · Sep 14, 2016 at 12:35 PM
I recently migrate to unity 5.4 I have the same issue. To me, migrate to newer version is not a good think. I always have issues. I flip the character with negative scale. What other way I could do this if are 3d or 2d. I wil try to rotate, ou flip 2d sprite. Thanks!