- Home /
Snap point to point on another prefab
Hi all,
I'm looking for general advice on how best to go about attaching prefabs to other prefabs,
let's say for example I have a prefab Torso, with nodes/points set for the shoulders (maybe empty game objects),
and I have prefab arms with their own nodes/points that I want to attach to the shoulders and for the prefab to snap correctly point to point ?
I've also started thinking what if you had a few room prefabs with doors in certain spots, and if you try to click the rooms together on a grid, the doors would need to line up (so it would be similar to the previous arm-shoulder-socket thing)
also (if possible) - is there a way to combine prefabs into a new prefab ?
I couldn't find anything in the Answers database because I wasn't sure how best to phrase the question. In the UDK I think they call this sort of thing 'sockets' but in Unity searches, that brings me to a load of networking questions.
Any help at all would be much appreciated - just the general idea, I probably won't need the code, should be able to figure it out.
I hope I've made my idea clear enough. Thanks
Answer by syclamoth · Sep 14, 2011 at 01:22 PM
If all you need is for 'sockets' to line up well, try having empty transforms on all the points where you need attachments, and then scripting your objects to align themselves with the empties (possibly setting up transform parents to keep them together). If you need to do something more complex (for example, aligning fully functioning limbs on an existing, invisible skeleton), then things get a little more difficult, but still possible to do. My solution to this problem was to make sure that all the replacement limbs had the same hierarchy and structure, and then in the replacement step checked through the hierarchy of the parent object and made every node a child of the node in the parent with the same name. To detach them, I kept a list of the original transform parents from before they were attached, and reinstated them to return the limb to its original state.
As for your prefabs question, have a look thought the Editor section of the unity documentation- there are several classes and functions to do with prefabs that could be useful to you.
Great answer syclamoth - exactly what I was looking for. I'll take a look at the Editor section of the docs as well... Thanks!
one more question to do with the above - when trying to find nearby sockets, I'm doing a physics.overlapsphere on the 'sockets' layermask (layer 8 I put them on) - but because the 'sockets' are within a prefab the overlapsphere is only getting the prefab collider, should I 'explode' / break the prefab into seperate parts before doing physics.overlapsphere? (and if so, how do I do that ?)
This is a bit out of my depth, I'm afraid- it's not really the way I'd do it, and I'm really not sure how those interactions work!
it's ok, I figured it out - you just set transform.parent=null to break the link to a parent, and allow the child to spread its wings, and go forth into the world to find its own crappy job and fend for itself. :-D
oh and the sockets were just empty gameobjects and I couldn't figure out why physics.overlapshere wouldn't find them even on the layermask given, but it was because they didn't have any colliders attached (it took me quite a while to figure that one out!)
Your answer
![](https://koobas.hobune.stream/wayback/20220612212754im_/https://answers.unity.com/themes/thub/images/avi.jpg)