【问题标题】:Unity3D: Bring child objects in it's original position after it is translated and rotatedUnity3D:在平移和旋转后将子对象置于其原始位置
【发布时间】:2020-08-18 00:23:31
【问题描述】:
  1. 这是立方体及其子对象圆锥的原始位置。

  1. 在这里,我将圆锥体翻译了 2 个单位

  2. 现在我已将父立方体沿 Y 轴旋转了 25 度

  3. 如果我们再次将圆锥体平移 2 个单位,它就不是在原来的位置

在将锥体平移回原来的位置时,我需要对它应用什么公式?

【问题讨论】:

    标签: c# unity3d rotation parent-child translation


    【解决方案1】:

    如果您在 Unity 中使用 transform.Translate() 方法移动对象并且希望保持相对于它的父对象,那么执行此操作的一种方法是将父对象的变换传递给空间参数Translate.

    像这样:

    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 项目中重新创建了您的问题,并使用上述方法修复了它。

    【讨论】:

    • 谢谢!有效。虽然,是否可以相对于父级使用 Vector3.Lerp()?
    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    相关资源
    最近更新 更多