Home > database >  How to move child object to parent's location?
How to move child object to parent's location?

Time:05-02

How would I go about teleporting my child object to it's parent's location without a constant parent? (Parent keeps changing)(c#)(Unity).

CodePudding user response:

You can try this

void UpdateChildParent(Transform newParent, Transform child)
{
  var cPos = child.localPosition; //Save old local position of Child
  child.SetParent(newParent); //Switch to new parent
  child.localPosition = cPos; // copy old local position of child
}

CodePudding user response:

If the child doesn't have any other object between it and the parent, you could do :

child.transform.localPosition = new Vector3(0, 0, 0);

The local position is the position of an object, relative to its parent. So setting it to 0 will make the child go to its parent position.

  • Related