2D attaching Joint With Script ?
hi, 'm trying to Connect two 2d object with a C# Script , i have a Variable type Parts Gameobject[] ,adding the Parts Children, so i try link some objects to a Central object but i have a problem to get the Parts Childrens and do Link to the Central Object . Thanks to thoose who can help me :v
You mean you want to attach your objects together to become one object?
yes , think like a 2d ship building,that have parts to add to an Startin Block, and the player can Attach Parts in other parts.
Answer by TX Manager · Apr 02, 2016 at 07:48 PM
There's really no way to attach 2 objects together using unity because it's not a software that works with the mesh or the sprites (Imported info) so you can decleer a new empty game object like this :- public GameObject parentObject = New GameObject (Vectro3, Vector3);
set the transform as you like
public GameObject obj_1; public GameObject obj_2;
then set the 2 game objects to become Children of the parent obj
`void Update ()
{
obj_1.transform.parent = parentObject.transform;
obj _2.transform.parent = parentObject.transform;
}`
if you are using a sprite software to make this 2D game, it will be best to use this way. if you are using Mesh in 3D or 2D game, so you better do it by your own by setting a new empty object in the scene and get one of those 2 objs to it then set it to the project asset folder then put the other object and then drag and put it again into the project asset folder,
this simple trick can attach them both. try it or see "how to save a prefab into a file in the window" to do this trick I told you in the docs : Windows - write all bytes I can't teach this thing because I didn't try it before
I hope this will help :)
tell me if it helped or try to tell me what you really want to know more about this
@TX $$anonymous$$anager here is a better explanation of what I'm trying to:
this is very simple, I think.
1-set the sprite as a prefab on the Project
2-caluclate the distance between the 2 objects you want to connect like this :-
void Update ()
{
vector3 distance = New vector3 (firstobj.transform.x - secobj.transform.x, firstobj.transform.y, secobj.transform.y, 0f);
}
3-then Instantiate the prefab where it should be and calculate the scale by your own to know how scale do you want by the distance
I can't tell you more the details because I don't know the joint scale or how does it like
just calculate the distance then tell the computer in the script that the game object will be Instantiate between the 2 objects
"1.transform - 2.transform", then set the scale 2 eqal the
"1.transform - 2.transform * //the num you will know by your exper$$anonymous$$ts in the unity editor", I hope you will understand :)