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. Dismiss Notice

Completely inverse the transform of a child GameObject

Discussion in 'Scripting' started by VesuvianPrime, Jul 10, 2014.

  1. VesuvianPrime

    VesuvianPrime

    Joined:
    Feb 26, 2013
    Posts:
    135
    Hey guys

    I have a GameObject that I want to always have transform 0, rotation 0, scale 1, completely regardless of any parent transformation.

    Setting world position and rotation is extremely easy, but figuring out the scale not so much.

    Code (csharp):
    1. // Flatten out the transform
    2. child.transform.position = Vector3.zero;
    3. child.transform.rotation = Quaternion.identity;
    4. child.transform.localScale = ???;
    How can I completely inverse the transform of my object, considering the full hierarchy of transforms, to ensure my GameObject is ALWAYS sat on the origin with no rotation and scale of 1?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why does it need to be a child? given that you want it to be effectively independent of the parent?
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    To reverse the sclae you have to divide the current localScale by all of its hierarchy parents scale.

    example

    Go1 Scale: 2 , 2 , 2
    Go1 Scale: 4 , 4 , 4
    TargetGO Scale: 1 , 1 , 1​

    after calculation

    Go1 Scale: 2 , 2 , 2
    Go1 Scale: 4 , 4 , 4
    TargetGO Scale: 0,125 , 0,125 , 0,125​
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Even then, that's not going to be reliable. If the parents are rotated, it's going to be all wrong.

    I think this might actually be fundamentally impossible (or at least fundamentally unreliable) - it's the reason that transform.lossyScale exists as it does, because there are situations where it's not possible to accurately describe an object's scale if the parents are rotated and scaled.

    For example, if you have this hierarchy:
    Parent (rotated 45,0,0)
    Child (scaled 1,0.25,1)
    Grandchild
    What scale value could the Grandchild have that could POSSIBLY cancel out its parent? There is none.

    LeftyRighty is correct, this thing just shouldn't be a child.
     
  5. VesuvianPrime

    VesuvianPrime

    Joined:
    Feb 26, 2013
    Posts:
    135
    Had a few more beers and came to the same conclusion. Ive been obessively using GameObjects as folders to keep things tidy, but I was already hiding my child anyway, so it doesn't matter if it isn't parented to anything.

    Since my object was always going to be at the origin with a position of Vector.zero and a default rotation, I don't think world scale calculations would have been too complicated. I got about as far as writing the following:

    Code (csharp):
    1.    /// <summary>
    2.    ///    TransformExtensions provides extension methods for working with Transforms.
    3.    /// </summary>
    4.    public static class TransformExtensions
    5.    {
    6.      /// <summary>
    7.      ///    Gets the world scale.
    8.      /// </summary>
    9.      /// <returns>The world scale.</returns>
    10.      /// <param name="extends">Extends.</param>
    11.      public static Vector3 GetWorldScale(this Transform extends)
    12.      {
    13.        Vector3 parentScale = Vector3.one;
    14.        if (extends.parent != null)
    15.          parentScale = GetWorldScale(extends.parent);
    16.  
    17.        return Vector3.Scale(parentScale, extends.localScale);
    18.      }
    19.  
    20.      /// <summary>
    21.      ///    Sets the world scale.
    22.      /// </summary>
    23.      /// <param name="extends">Extends.</param>
    24.      /// <param name="scale">Scale.</param>
    25.      public static void SetWorldScale(this Transform extends, Vector3 scale)
    26.      {
    27.        Vector3 worldScale = extends.GetWorldScale();
    28.  
    29.        Debug.Log(worldScale);
    30.  
    31.        //extends.localScale = new Vector3(scale.x / worldScale.x,
    32.        //                   scale.y / worldScale.y,
    33.        //                   scale.z / worldScale.z);
    34.      }
    35.    }
    Not quite right, but I think I was close to getting what I wanted.