- Home /
Can you mirror a bunch of gameobjects ?
Hi all
I suddenly would like the ability to mirror a bunch of gameobjects, in my case with colliders attached.
any ideas?
In reality it is because I made some colliders manually for a rounded pair of stairs, and just want to copy those colliders to the other side. I tried scaling the colliders in a negative direction, but didn't work, so I guess I just have to do it manually again :)
sigh it's really confusing when people post questions, and it turns out the question is utterly and totally unrelated to what it sounds like the question is about
I guess sometimes it is just hard to put the questions right.. my bad, edited the question
Answer by Kmulla · Nov 21, 2012 at 11:44 AM
Hi Jake :)
It's possible to just set all the scale values to -1 and then rotate your gameobject in the desired axis by 180 degrees.
Thanks $$anonymous$$mulla... that one worked! I guess I just didn't see it from the beginning
Wow. Thank you. I have been using Unity a long time now and I NEVER ONCE even thought of that. Crazy how obvious it seems now.
Wow - this is an amazing solution and saved me from a huge headache! Thanks, $$anonymous$$mulla!
Thank you! This solved a problem that would've had me stumped for hours, if not days!
It has a huge advantage over simply flipping the desired axis, that it does not mess up future transform operations done by the mirrored object!
Answer by toomasio · Jan 30, 2018 at 09:21 PM
Here's one way to mirror objects without adjusting the scale. You can use the public method to switch the master objects that you wish to manipulate.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MirrorGameObjects : MonoBehaviour
{
public List <Transform> objsToMirror = new List<Transform>();
public Transform slaveParent;
private Transform[] masterObjs;
private Transform[] slaveObjs;
// Use this for initialization
void Start ()
{
CreateSlaves();
}
// Update is called once per frame
void FixedUpdate ()
{
MirrorObjects();
}
void CreateSlaves()
{
if (!slaveParent)
slaveParent = transform;
if (objsToMirror.Count < 1)
{
foreach (Transform child in transform)
{
objsToMirror.Add(child);
}
}
masterObjs = objsToMirror.ToArray();
slaveObjs = new Transform[masterObjs.Length];
for (int i = 0; i < objsToMirror.Count; i++)
{
slaveObjs[i] = Instantiate(masterObjs[i]);
slaveObjs[i].SetParent(slaveParent);
}
}
void MirrorObjects()
{
for (int i = 0; i < objsToMirror.Count; i++)
{
slaveObjs[i].localPosition = new Vector3(-masterObjs[i].localPosition.x, masterObjs[i].localPosition.y, masterObjs[i].localPosition.z);
slaveObjs[i].localEulerAngles = new Vector3(masterObjs[i].localEulerAngles.x, -masterObjs[i].localEulerAngles.y, -masterObjs[i].localEulerAngles.z);
}
}
public void SwitchMaster()
{
Transform[] temp = masterObjs;
masterObjs = slaveObjs;
slaveObjs = temp;
}
}
Your answer
Follow this Question
Related Questions
uGUI WorldSpace elements part of GameObjects 0 Answers
selecting and organising objects in editor 0 Answers
Fold/unfold gameobject from code 4 Answers
Keep adding targets to a list 2 Answers
Rotating a Vector3 in Instantiation 1 Answer