Search code examples
unity-game-engine3daugmented-realityfbxprefab

How we can move two together in unity 3D?


Actually I want to connect two child object with each other, so that when size increase of one object both the object should be moved together and change its position. I want to do this in prefab is it possible ?

This is hierarchy screenshot:

enter image description here

Basically I want to increase size of glass (child object of side1_screen1) but along with glass, my support(child object) its size is also increasing.

But when individually I increase size of glass my support position is not changing, so any approach that can be used when glass size increases, my support size should not increase.

Any approach will be appreciated. Thanks in advance.


Solution

  • I do not know if this is what you are asking for, but you can simply "connect" multiple child objects by parenting them to the same object. In this example, I have simply created two child objects and an empty GameObject as container. Whenever you scale, rotate or move the container, the child objects will do the same.

    Example of parenting

    In the hierarchy, you cannot "connect" multiple child objects to each other without also making one of them a parent.

    Another (quick and dirty) approach you could try is to create a script that references the transform component of both GameObjects and then sets one of them according to the other.

    You can place this inside your Update() method:

    firstChild.transform.position = secondChild.transform.position + Vector3 offsetValue (otherwise they will be in the same position)
    
    firstChild.transform.rotation = secondChild.transform.rotation
    
    firstChild.transform.localScale = secondChild.transform.localScale
    

    After you've created your script and referenced everything to your liking, you can simply drag the parent object into one of your project folders to make it a prefab.