MrRobot9
MrRobot9

Reputation: 2684

Unity3D: Bring child objects in it's original position after it is translated and rotated

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

enter image description here

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

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

  3. 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?

Upvotes: 0

Views: 366

Answers (1)

Garrison Becker
Garrison Becker

Reputation: 482

If you're moving an object using the transform.Translate() method in Unity and you want to stay relative to it's parent, then one way to do this is to pass in the transform of the parent for the Space parameter in Translate.

Like so:

var parent = transform.parent;

transform.Translate(0, 0, 2, parent); // 2. Over here, I have translated the cone for 2 units
parent.Rotate(0, 25, 0); // 3. Now I have rotated the parent cube around 25 degrees wrt to the Y-axis

transform.Translate(0, 0, -2, parent); // 4. If we translate the cone 2 units back again, it's now in the original position

I recreated your issue in a new Unity project and fixed it using the above.

Upvotes: 1

Related Questions