Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Get local transform info from non-child

Discussion in 'Scripting' started by wwu39, Jan 24, 2022.

  1. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    25
    So I have 2 objects: A and B. How can I get A's local position and rotation related to B as if A is a child of B without actually making A a child of B?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    Given A & B are Transform components then this should work (not tested):
    Code (CSharp):
    1. var localPosA = B.InverseTransformPoint(A.position);
    2. var localRotA = Quaternion.Inverse(B.rotation) * A.rotation;
    ... although this doesn't calculate scaling but you didn't mention that. You can set the A local scale to be the reciprocal of the B lossyScale (1/lossyScale) * A lossyScale.
     
    Bunny83 likes this.
  3. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    25
    Thought I would need some math tutoring. Turns out Unity did math for me!