Search Unity

Custom pivot for a gameobject

Discussion in '2D' started by Oleksandr123, Aug 9, 2019.

  1. Oleksandr123

    Oleksandr123

    Joined:
    Jan 15, 2018
    Posts:
    3
    Below is a picture depicting "I", "J", "L" game objects. Each object has children. These children are blocks and a pivot.
    I want to instantiate game objects relative to their child Pivot, but I do not how to do it.

    I have a script that instantiates these objects
    Code (CSharp):
    1.  
    2.     public GameObject[] Shapes;
    3.     public Vector2 beginPoint;
    4.     public Vector2 resize;
    5.     public float offsetBetweenBlock;
    6.     private const int numberToShow = 3;
    7. ...
    8. for(int i = 0; i < numberToShow; i++)
    9.         {
    10.             GameObject currentShape = Shapes[Random.Range(0, Shapes.Length)];
    11.             Instantiate(
    12.                 currentShape,
    13.                 new Vector3(beginPoint.x + i * offsetBetweenBlock, beginPoint.y),
    14.                 Quaternion.identity,
    15.                 transform
    16.                 );
    17.         }
    But as you can see "J"(Medium) and "L"(Right) are not equal to each other (I pointed at them with red arrows on the picture)

    How can I instantiate objects "I", "J", "L" with respect to their child Pivot?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Make the "Pivot" be the root parent object and place the children relative to it. If you're cloning a prefab then create a root pivot object and set the cloned prefab as a child.
     
    bloodwolftico and Oleksandr123 like this.
  3. Oleksandr123

    Oleksandr123

    Joined:
    Jan 15, 2018
    Posts:
    3
    Thanks! It is so easy. Sorry for my question
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    You're welcome and never be sorry asking questions, it's the only way we can all learn. :)
     
    bloodwolftico likes this.