GameObject 获取相对于祖父母的位置

Ceo*_*ndo 1 unity-game-engine

transform.localPosition给我相对于父级的位置。transform.parent.transform.localPosition给我父母相对于祖父母的位置。我需要的是子对象的本地位置,但相对于祖父母而不是相对于父对象。如何才能团结一致地实现这一目标?

NSJ*_*ob1 6

您可以使用Transform.InverseTransformPoint获取任何变换的局部空间中的任何世界位置。

    Vector3 grandParentLocalPosition = transform.parent.parent.InverseTransformPoint(transform.position);
Run Code Online (Sandbox Code Playgroud)

这将得到祖父母变换 ( transform.parent.parent),然后将当前对象 ( ) 的世界位置transform.position放入祖父母的局部空间中。

  • @Ceolando它为您提供了孙子(`transform.position`)相对于祖父母(`transform.parent.parent`)的位置和旋转的位置。 (2认同)