- Home /
Attaching multiple objects to one object with script.
Okay, kind of complex question here. Long story short, I'm fairly new. I started this project without realizing quite how difficult a task this section was going to be, and now I'm fairly determined to finish.
Here is the deal, I have a block that the player controls. I have code to be able to pick up and drag other objects on the field, but the final thing I need is some way to attach these objects to the main object and to each other. The goal is to have user made vehicles by picking up and dragging unowned bits on the field and sticking them on your own ship. The game is top down in a 2D environment with 3D objects. So, 4 joints are needed on every object, or so it seems.
The question is.... what's the simplest way I can handle this? My current idea for this involves using 4 joints, one for each side of the object (all objects are cubes, though they have separate functions.) However, joints can't have names, because they are components. I know I can use a function to return all of the joints in a list, and then remember the order they are returned in, but this seems like a really sketchy way of doing this. On top of that, I'm not quite sure how to do the code to fill the joints with the correct object to begin with. I may have to have every object have 4 separate rays? That seems terribly unorganized, or at very least like a lot to have in the background.
I can't use a parent/child relationship as far as I know, because the physics involved is important. Objects need to weigh the player down. For instance, a ship with one wing is going to fly in circles. This works very very well with hinges as far as I've seen from testing, but the player actually attaching objects seems incredibly difficult for me through script.
Is there an easier way of doing this that I'm not seeing? If not, how can I solve these problems? I'm sure it would be a simple job for a pro, but I am still learning. Even so, I feel it's important to challenge myself with something like this here and there, but I've been stuck on this segment for a solid week, so I am seeking help haha.
I realize this was a long post. Even if you don't have a solution for me, thank you for reading.
Answer by StephanK · Nov 03, 2010 at 08:44 AM
Your ship's controller script could contain an array with references to its 4 joints. Then you could define which index contains which joint and you could use that to attach objects to the correct joints. If you want cleaner code you could make an enum for the joint indexes so you could write something like this in your code: controller.joints[JointIndex.right]
Thanks, that solved almost all of my problems, but I'm fairly sure I can take it from here. You've been a great help <3.
Your answer
Follow this Question
Related Questions
How I can create a Javascript object in a unity Script? 1 Answer
Attach object to avatar bone 1 Answer
script dosnt creat object where i want it to 1 Answer
Can't get more than one object. 1 Answer
Create object inside a game 1 Answer