简体   繁体   English

Unity3D:平移和旋转后将子对象带入其原始 position

[英]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.这是立方体的原始 position 及其子圆锥体 object。

在此处输入图像描述

  1. Over here, I have translated the cone for 2 units在这里,我已经翻译了 2 个单位的圆锥体在此处输入图像描述

  2. Now I have rotated the parent cube around 25 degrees wrt to the Y-axis现在我已经将父立方体相对于 Y 轴旋转了 25 度在此处输入图像描述

  3. If we translate the cone 2 units back again, it's not in the original position如果我们再次将锥体平移 2 个单位,它就不是原来的 position 在此处输入图像描述

What formula do I need to apply to the cone while translating it back to bring it back to it's original position?在将其转换回原来的 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 .如果您在 Unity 中使用 transform.Translate() 方法移动 object 并且您希望保持相对于它的父级,那么一种方法是为 Translate 中的 Space 参数传入父级的转换

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.我在一个新的 Unity 项目中重新创建了您的问题,并使用上面的方法修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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