Child parent 旋转时位置不变
Child position not changing when parent rotates
我觉得很简单,但是我一直在网上寻找解决方案,但找不到合适的结果-如果已经写好了,请见谅。
我有一个球体,它使用一个简单的脚本在 Y 轴上缓慢旋转:
transform.Rotate(0, Time.deltaTime, 0);
我有一个 child object 附在这个球体上,它随着 parent 旋转。到目前为止一切顺利。
我已将枢轴放置在 child 的中心,并且我试图获取 child object 的位置,因为它使用 parent:
void Update ()
{
Debug.Log(this.transform.position);
}
但是,更新提供了 child 的位置,就好像它与 parent 没有关联一样。
你们能告诉我在更新时附加到球体 parent 时获取 child 位置的方向吗?
感谢您的时间和耐心等待。
不确定是否真正理解 "the position of the child when attached to the sphere parent as it updates" 的意思,但我会试一试。
我想你的层次结构是这样的:
-- Sphere (rotating along its Y axis)
-- Child (that rotates with "Sphere" parent)
-- Pivot (in the center of the "Child" object and rotating along its Y axis)
-- Child2 (that rotates with "Pivot" parent)
如果你想获得 Child2 相对于 Sphere 的位置,你可以这样做:
Vector3 relativePosition = Child2.transform.position - Sphere.transform.position;
您还可以使用 Transform.localPosition 获取对象相对于其父对象的位置。在这种情况下,您可以这样使用它:
Vector3 relativePosition = Child.transform.localPosition + Pivot.transform.localPosition + Child2.transform.localPosition
希望对您有所帮助,
我觉得很简单,但是我一直在网上寻找解决方案,但找不到合适的结果-如果已经写好了,请见谅。
我有一个球体,它使用一个简单的脚本在 Y 轴上缓慢旋转:
transform.Rotate(0, Time.deltaTime, 0);
我有一个 child object 附在这个球体上,它随着 parent 旋转。到目前为止一切顺利。
我已将枢轴放置在 child 的中心,并且我试图获取 child object 的位置,因为它使用 parent:
void Update ()
{
Debug.Log(this.transform.position);
}
但是,更新提供了 child 的位置,就好像它与 parent 没有关联一样。
你们能告诉我在更新时附加到球体 parent 时获取 child 位置的方向吗?
感谢您的时间和耐心等待。
不确定是否真正理解 "the position of the child when attached to the sphere parent as it updates" 的意思,但我会试一试。
我想你的层次结构是这样的:
-- Sphere (rotating along its Y axis)
-- Child (that rotates with "Sphere" parent)
-- Pivot (in the center of the "Child" object and rotating along its Y axis)
-- Child2 (that rotates with "Pivot" parent)
如果你想获得 Child2 相对于 Sphere 的位置,你可以这样做:
Vector3 relativePosition = Child2.transform.position - Sphere.transform.position;
您还可以使用 Transform.localPosition 获取对象相对于其父对象的位置。在这种情况下,您可以这样使用它:
Vector3 relativePosition = Child.transform.localPosition + Pivot.transform.localPosition + Child2.transform.localPosition
希望对您有所帮助,