How to get local position & rotation from not parent object

Hello,

My scene has three GameObjects, A, B and X.

X’s parent is A.

B is unrelated to A, move and rotate uniquely.

I want to know local position & rotation if X’s parent is B.

Therefore, first I set X’s parent to B temporarily, get local positon & rotation, and set X’s parent to A again.
But cost of changing parent is large.

So second, I create dummy object and set it’s parent to B.
And set dummy’s world positon & rotation equal to X’s world position & rotation and get dummy’s local positon & rotation.
This way works fine, but cost is not small.

Is there a way more light?

Sorry my English is limited. I thank you for reading it through.

From memory, local position of X relative to B is B.inverseTransformPoint(X.position);.

For local rotation, you “subtract” B’s rotation from X, so (this is probably wrong, but something like it works): Quaternion.Inverse(B.rotation)*X.rotation;.

Inverse is like using a “times -1” on the rotation, and * adds them together (math guys told us to use that symbol.) The order matters (so putting X.rotation first might be correct. I always have to experiment by testing at easy angles.)