Search Unity

Moving position of child gameobject

Discussion in '2D' started by inthemilkywat, Aug 22, 2016.

  1. inthemilkywat

    inthemilkywat

    Joined:
    Dec 29, 2013
    Posts:
    56
    So I have object A which has a child object B. I want to move the child object B to the position of another object C. I still want to keep object B as a child of object A. I tired changing localposition but that is only relative to the parent object.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can set the regular position. It will remain as a child.

    objectB.position = objectC.position;
     
  3. inthemilkywat

    inthemilkywat

    Joined:
    Dec 29, 2013
    Posts:
    56
    I tried that but the object is no where near the target position.

    EDIT: Oh I figured it out. The position was not correct because I was scaling the parent gameobject.
     
    Last edited: Aug 22, 2016
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can try this: objectB.position = parentTransform.InverseTransformPoint(objectC.position);

    That will convert the objectC.position into the local space for the parentTransform, and then assign it to objectB