# c# – Flipping 3D Gameobjects in Unity3d

## c# – Flipping 3D Gameobjects in Unity3d

Yes, flipping an object is not that hard actually. Heres two snippets of my code that achieve this (I left out the obvious boilerplate that grabs the mesh reference):

``````    public bool flipX = true;
public bool flipY;
public bool flipZ;

void Flip()
{
if (mesh == null) return;
Vector3[] verts = mesh.vertices;
for (int i = 0; i < verts.Length; i++)
{
Vector3 c = verts[i];
if (flipX) c.x *= -1;
if (flipY) c.y *= -1;
if (flipZ) c.z *= -1;
verts[i] = c;
}

mesh.vertices = verts;
if (flipX ^ flipY ^ flipZ) FlipNormals();
}
``````

One important detail is that depending on the axis configuration you might need to flip face normals. This is achieved by reversing the order in which verts are referenced in the triangles array:

``````    void FlipNormals()
{
int[] tris = mesh.triangles;
for (int i = 0; i < tris.Length / 3; i++)
{
int a = tris[i * 3 + 0];
int b = tris[i * 3 + 1];
int c = tris[i * 3 + 2];
tris[i * 3 + 0] = c;
tris[i * 3 + 1] = b;
tris[i * 3 + 2] = a;
}
mesh.triangles = tris;
}
``````

You can scale the object by `X` = `-1` or `Y` = `-1` or `Z` = `-1` in inspector. According to the axis you want to mirror the object.

By code you can do this by:

``````transform.localScale += new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
``````

for mirroring by the x-axis for example.

This is working for simple objects. If the object is animated this will cause a strange behaviour.