unity 【數學】 不使用TransformPoint計算當前localPosition對應的worldpos
求一個localPos的worldPos,如果能拿到parentTransform, 可以使用
一 :Transform.TransformPoint 和 Transform.TransformDirection 獲取worldPos和worldDir
二:還有一種Matrix的方式:https://answers.unity.com/questions/1273879/local-point-to-world-point-without-transformpoint.html
1.Matrix4x4 m = Matrix4x4.TRS(position, rotation, scale);獲取parent的Matrix
2.Vector3 worldPos = m.MultiplyPoint3x4(localPos); 獲取localPos的世界pos
3.Vector3 localPos = m.inverse.MultiplyPoint3x4(worldPos);根據子物體的worldpos獲取子物體的localPos
同理可以通過Matrix獲取rotate和scale的計算。
如果roate和scale都是default值,則pos的計算可以直接:Vector3 worldPos = localPos + position;

浙公網安備 33010602011771號