# How do I bring a child object in its original position after it is translated and rotated?

1. This is the original position of the cube and its child object cone.

2. Over here, I have translated the cone for 2 units

3. Now I have rotated the parent cube around 25 degrees wrt to the Y-axis

4. If we translate the cone 2 units back again, it's not in the original position

What formula do I need to apply to the cone while translating it back to bring it back to it's original position?

Code: The below code is a coroutine for translating the child object

  IEnumerator MoveToNewPos()
{
Vector3 newPos += Quaternion.Euler(0, 0, 135) * Vector3.left * distance;

float t = 0;
float animTime = 30;
isMoving = true;
for (; Vector3.Distance(transform.position, newPos) > 0.001f; t += Time.deltaTime)
{
float p = t / animTime;
transform.position = Vector3.Lerp(transform.position, newPos, p);
yield return null;
}
isMoving = false;
}

• Can you show us what code you're using to move the child object around? It looks like you might be manipulating its world position, instead of its localPosition. Aug 18 '20 at 14:35
• @DMGregory I have added the code to the question Aug 18 '20 at 15:21
• @DMGregory It's in a coroutine actually Aug 18 '20 at 15:22
• Then the code you show in your question should make that context clear, so a reader doesn't need to dig into the comment thread to learn something your code could have simply included. Aug 18 '20 at 16:01
• @DMGregory Thank you! Made it clear now. Aug 18 '20 at 16:14

This is probably easiest if you work in terms of localPosition instead of position.

localPosition tracks your offset relative to the parent coordinate space, so it's unaffected by the parent's rotation.

I also modified your routine below to make the rate of movement linear, since that's what your p calculation seemed to assume, but your Lerp was inconsistent with this.

IEnumerator MoveToNewPos(Vector3 destination)
{
Vector3 origin = transform.localPosition;

float animTime = 30;
isMoving = true;
for (float t = 0; t < 1f; t += Time.deltaTime/animTime)
{
transform.localPosition = Vector3.Lerp(origin, destination, t);
yield return null;
}

transform.localPosition = newPos;
isMoving = false;
}


Then you can move out with...

// Cache in a member variable, so you can return to this exact position later.
startPosition = transform.localPosition;
Vector3 newPosition = startPosition
+ Quaternion.Euler(0, 0, 135) * Vector3.left * distance;

StartCoroutine(MoveTo(newPosition));


And move back with...

StartCoroutine(MoveTo(startPosition));


The answer to your question "How do I bring a child object in its original position after it is translated and rotated?" is that you apply the opposite transforms in the reverse order that you applied them.

If you've applied the following transforms to an object:

1. rotate n degrees, and
2. translate by vector v,

you return it to it's original position and rotation by:

1. translate by vector -v
2. rotate by -n degrees

The reason your object isn't returning to it's original position and rotation is because there is a problem/bug in your code or object hierarchy that you haven't shared with us.

If you'd like further help solving your problem, please review How to create a Minimal, Reproducible Example and follow the guidelines for using this site.

• Please note that we have "our own version" of that page here. Aug 18 '20 at 23:15