简体   繁体   中英

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.

在此处输入图像描述

  1. Over here, I have translated the cone for 2 units在此处输入图像描述

  2. Now I have rotated the parent cube around 25 degrees wrt to the Y-axis在此处输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM