- Home /
Vector3.Scale and rotated colliders
I'm building a puzzle game, and I would like to horizontally flip pieces randomly when instantiated via code. Each piece has multiple box colliders and mesh objects as children to a container object.
I've tried using Vector3.Scale on my container object to flip everything at once by scaling in X by -1. Things looks great at first glance, but any of my collider objects that are rotated do not come through correctly.
The (rotated) meshes for the collider objects (just there to help me visibly see what's going on) flip properly, but their colliders no longer match their size and orientation with the mesh. They keep their original orientation and scale down somewhat(?).
Here's the code I am using:
Vector3 flipVector = new Vector3(-1, 1, 1);
transform.localScale = Vector3.Scale (transform.localScale, flipVector);
Does anyone know a good way around this? I can manually rotate/scale the individual elements if I have to, but that seems like a lot of extra work...
Why not just rotate the container object? There are lots of ways. Here's one
transform.eulerAngles = new Vector3(180,0,0);
or since you are using localScale, maybe you need:
transform.localEulerAngles = new Vector3(180,0,0);
This flips it over on the x axis.
Simply rotating the container creates other problems for me, because the z-values of the elements within it need to be maintained with respect to the world (hard to explain). So I'd still have to do a lot of manual fixes.
Answer by Backward pieS · Mar 15, 2013 at 12:49 AM
Okay, that wasn't so bad.
What I did was handle the individual components separately. Those that no offset or rotation could simply be scaled by -1 in X to be flipped.
The rotated elements needed got a local 180 spin, then I used Reflect on their localPosition. Offset colliders that weren't rotated got the same Reflect treatment but without the spin.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094432im_/https://answers.unity.com/themes/thub/images/avi.jpg)