Skip to content

Instantly share code, notes, and snippets.

@SDraw
Last active August 2, 2024 20:07
Show Gist options
  • Save SDraw/09e8f09431fe324caa8936ddfb6630ba to your computer and use it in GitHub Desktop.
Save SDraw/09e8f09431fe324caa8936ddfb6630ba to your computer and use it in GitHub Desktop.
Simple utility class to mirror Unity's transforms along global planes
using UnityEngine;
public static class MirrorUtils
{
public enum MirrorPlane
{
OXY = 0,
OXZ,
OYZ
}
public static void MirrorAlongPlane(Transform p_source, Transform p_target, MirrorPlane p_plane)
{
Vector3 l_pos = p_source.position;
Quaternion l_rot = p_source.rotation;
switch(p_plane)
{
case MirrorPlane.OXY:
{
l_pos.z *= -1f;
Swap(ref l_rot.x, ref l_rot.z);
Swap(ref l_rot.y, ref l_rot.w);
l_rot.x *= -1f;
l_rot.z *= -1f;
}
break;
case MirrorPlane.OXZ:
{
l_pos.y *= -1f;
Swap(ref l_rot.x, ref l_rot.y);
Swap(ref l_rot.z, ref l_rot.w);
}
break;
case MirrorPlane.OYZ:
{
l_pos.x *= -1f;
l_rot.y *= -1f;
l_rot.z *= -1f;
}
break;
}
p_target.position = l_pos;
p_target.rotation = l_rot;
}
public static void Swap<T>(ref T a, ref T b)
{
T l_temp = a;
a = b;
b = l_temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment