Comments and answers for "Modifying the Relative Orientations of Two Cubes"
http://answers.unity.com/questions/1220962/modifying-the-relative-orientations-of-two-cubes.html
The latest comments and answers for the question "Modifying the Relative Orientations of Two Cubes"Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1221701/view.html
Great since i haven't tested it :DWed, 27 Jul 2016 15:12:43 GMTBunny83Comment by rish209 on rish209's answer
http://answers.unity.com/comments/1221260/view.html
Thanks for the help. This seems to work perfectly.Tue, 26 Jul 2016 17:02:31 GMTrish209Answer by Bunny83
http://answers.unity.com/answers/1221118/view.html
This should do what you want:
private static Vector3 AxisAlign(Vector3 aVec)
{
var size = new Vector3(Mathf.Abs(aVec.x), Mathf.Abs(aVec.y), Mathf.Abs(aVec.z));
if (size.x > size.y)
{
if (size.x > size.z)
return Vector3.right * Mathf.Sign(aVec.x);
}
else if (size.y > size.z)
return Vector3.up * Mathf.Sign(aVec.y);
return Vector3.forward * Mathf.Sign(aVec.z);
}
public static void AxisAlignTransform(Transform aTrans, Transform aReference)
{
Vector3 f = AxisAlign(aReference.InverseTransformDirection(aTrans.forward));
Vector3 u = AxisAlign(aReference.InverseTransformDirection(aTrans.up));
f = aReference.TransformDirection(f);
u = aReference.TransformDirection(u);
aTrans.rotation = Quaternion.LookRotation(f,u);
}
Just call AxisAlignTransform and pass your cube you want to align as first parameter and the reference object as second parameter.
The trick here is to simply convert the target cube's forward and up vector into the local space of the reference transform. Now they are local vectors and we just need to find the largest value of the 3 vector components as that's the major direction and will be the closest direction we want.
Once axisaligned we just convert the local vectors back into worldspace where we can use them in LookRotation to calculate the new orientation.Tue, 26 Jul 2016 11:48:59 GMTBunny83