存储子项相对于其父项的旋转在 Unity3d 中无法正常工作

Storing rotation of child relative to its parent not working properly in Unity3d

我正在尝试存储子游戏对象的相对旋转,以便我可以在需要时计算它们的世界旋转,因为我正在删除那些因为没有用的东西,为此我正在存储从它们的前向到父级的前向的旋转, 但它似乎没有用。

// store relative rotation
var rot = Quaternion.FromToRotation(child.forward, parent.forward);

// get world rotation
var childWorldRot = parent.rotation * rot;

也许有涉及四元数转换的更好解决方案,但这很简单,应该可以解决问题:

// store relative directions
var refUp = parent.InverseTransformDirection(child.up);
var refForward = parent.InverseTransformDirection(child.forward);

// get world rotation
var childWorldRot = Quaternion.LookRotation(parent.TransformDirection(refForward), parent.TransformDirection(refUp));

只需使用Transform.localRotation。它是相对于父级的。
我不确定我是否完全理解您的最终目标是什么,但请记住 Transform.rotation 已经存在于世界 space.

// store relative rotation
var rot = child.localRotation;

// get world rotation
var childWorldRot = child.rotation;