- Home /
Programatically calculate child offset in world space
Hi All, Apologies if this has been answered elsewhere, with the terminology I know I can't find the answer.
I have an Offset in Position / Rotation relative to a Point. Call the Offset O and the Point A; I have O as a Matrix4x4 but I can get Vector3/Quaternions from that.
Point A could be anywhere, and be rotated.
I want to get the world space of O, offset from Point A.
I can solve this a hacky way... I spawn a Child GameObject under Point A, and set it's localTransform to the Offset O. I then take the world Position of that new Child.
Q: How can I do this in code? (I assume my way is horribly inefficient, and that a method using Vector maths would be the better solution)
Answer by villevli · Jan 29, 2018 at 04:18 PM
Seems like you want to get the world space transformation from the offset point relative to transform A.
Transform local to world position:
Vector3 offsetWorldPosition = pointA.TransformPoint(offsetLocalPosition);
Transform local to world rotation:
Quaternion offsetWorldRotation = pointA.rotation * offsetLocalRotation;
Calculate localToWorldMatrix:
Matrix4x4 offsetOfromA_localToWorldMatrix = pointA.localToWorldMatrix * offsetOMatrix;
Vector3 offsetWorldPosition = offsetOfromA_localToWorldMatrix * Vector3.zero;
Your answer
Follow this Question
Related Questions
Setting parent in instantiated class 3 Answers
Getting a Vector2 position depending on the rotation of an object 0 Answers
How to make custom handles take gameobject position offset into account? 0 Answers
Vector movement according to rotation glitching out at non-square angles. 0 Answers
Help! I want to make a following camera but in with Y position is constant ? 2 Answers