- Home /
Keeping (potentially hundreds) of object's transforms relative to animated counterparts? (Without Parenting)
I have two groups of objects: an animated one, and a non animated one. Both are identical in structure and my goal is to make each object in the non animated group welded to their respective counterpart in the identical animated group.
I am currently simulating this by updating the objects every frame in Update() to match their moving counterpart, but It's incredibly taxing on the engine. I'm trying to go for something that emulates the effect you get from a "parent child" relationship between transforms, only I need to keep them separate. Does a "Weld object A to always be relative to object B without parenting" method exist?
Answer by phxvyper · May 17, 2016 at 10:09 PM
This is the sort of thing I would do, in a controlling script that manages connections between objects and counterparts:
struct WeldConnection {
GameObject parent;
List<GameObject> children;
}
List<WeldConnection> Connections = new List<WeldConnection>();
void Update() {
foreach (WeldConnection connection in Connections) {
foreach (GameObject child in connection.children) {
child.transform.position = connection.parent.transform.position;
}
}
}
Even though its basically what you're doing already. Also, this sort of thing isn't that taxing on Unity. In fact, its one of the simplest and fastest operations. Take a look at the profiler in Unity to see how much CPU is being used here. This is a relatively reliable method of doing this.
Also, why do you need to keep them separate? You can always change parents via transform.SetParent
Thank you for the response. The reason I need them separate is because the objects in question are armature bones. What I am doing is attaching multiple objects with armatures and meshes (bone by bone) to an animated armature like an overlay. It's the current method I am using to have interchangeable body parts on a character. So I guess my end goal is simply to have a better method for doing the attaching.
Example:
I see parent-child used often. I'm certain you can figure out how to create that relationship in code. Do some googling around see if you can find any better suggestions than what I've given you.