Reputation: 9228
When in Unity I want to symmetrically mirror a transform along the x axis, for many shapes this works fine:
reflection.localRotation = new Quaternion(
original.localRotation.x * -1f,
original.localRotation.y,
original.localRotation.z,
original.localRotation.w * -1f
);
reflection.localPosition = new Vector3(
original.localPosition.x * -1f,
original.localPosition.y,
original.localPosition.z
);
But what would be the code for mirroring along the y axis? And what for mirror along the z axis? The positioning part is easy, but I'm wondering about the quaternion part. Thanks!
Upvotes: 0
Views: 9159
Reputation: 5035
modifying a quaternion by hand (treating it as an ordinary Vector4) as you do will not give you correct results. You'll be much better off modifying transform.eulerAngles (and as you want to mirror by a global axis I would imagine eulerAngles to be more apropriate than localEulerAngles but you could use either)
it still converts to a Quaternion internally but its just way easier to understand where you are in terms of rotation when using the Euler coordintate system
Upvotes: 0
Reputation: 4110
In 3d, you don't really mirror along an axis, but regarding a plane.
There is a thread in the Unity forum about this. One of the solution proposes a way to rotate not just for specific axis-aligned planes, but for any mirror plane represented by a normal.
var objectQuat = transform.rotation;
var plane = GetMirrorPlane(); // choose yours here
var mirrorNormalQuat = new Quaternion(plane.x, plane.y, plane.z, 0);
var reflectedQuat = mirrorNormalQuat * objectQuat * mirrorNormalQuat;
transform.rotation = reflectedQuat;
Upvotes: 2