Unity3D:平移和旋转后将child objects带回原来的位置
Unity3D: Bring child objects in it's original position after it is translated and rotated
- 这是立方体及其 child object 圆锥体的原始位置。
这边,我翻译了2个单位的圆锥体
现在我已经将 parent 立方体旋转了 25 度左右到 Y-axis
如果我们再将圆锥平移2个单位,它就不在原来的位置了
我需要将什么公式应用于圆锥体,同时将其平移以将其恢复到原始位置?
如果您在 Unity 中使用 transform.Translate() 方法移动一个对象并且您希望保持相对于它的父级,那么一种方法是将父级的转换传递给Translate.
中的 Space 参数
像这样:
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
我在一个新的 Unity 项目中重新创建了您的问题,并使用上面的方法修复了它。
- 这是立方体及其 child object 圆锥体的原始位置。
这边,我翻译了2个单位的圆锥体
现在我已经将 parent 立方体旋转了 25 度左右到 Y-axis
如果我们再将圆锥平移2个单位,它就不在原来的位置了
我需要将什么公式应用于圆锥体,同时将其平移以将其恢复到原始位置?
如果您在 Unity 中使用 transform.Translate() 方法移动一个对象并且您希望保持相对于它的父级,那么一种方法是将父级的转换传递给Translate.
中的 Space 参数像这样:
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
我在一个新的 Unity 项目中重新创建了您的问题,并使用上面的方法修复了它。