0
\$\begingroup\$
  1. This is the original position of the cube and its child object cone. enter image description here

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

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

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

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;
}
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Aug 18 '20 at 14:35
  • \$\begingroup\$ @DMGregory I have added the code to the question \$\endgroup\$
    – MrRobot9
    Aug 18 '20 at 15:21
  • \$\begingroup\$ @DMGregory It's in a coroutine actually \$\endgroup\$
    – MrRobot9
    Aug 18 '20 at 15:22
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Aug 18 '20 at 16:01
  • \$\begingroup\$ @DMGregory Thank you! Made it clear now. \$\endgroup\$
    – MrRobot9
    Aug 18 '20 at 16:14
1
\$\begingroup\$

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));
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Please note that we have "our own version" of that page here. \$\endgroup\$
    – Vaillancourt
    Aug 18 '20 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.