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

Transform.SetParent unexpect result?

Discussion in 'Scripting' started by Rob21894, Nov 19, 2016.

  1. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    I created a function that parents the Sticky Bomb to the first gameobject it hits on the layers. It casts an OverlapSphere until it finds the object.

    Code (CSharp):
    1.     public void detectClosestObj()
    2.     {
    3.         /* Code below checks for the closest object only on the bullet and enemy unit layer
    4.          * it will parent to the first collided object      
    5.          */
    6.         LayerMask layerMask = (1 << 10 | 1 << 16);
    7.         Collider[] checkObj = Physics.OverlapSphere(this.gameObject.transform.position, 0.1f, layerMask);
    8.  
    9.         List<GameObject> cols = new List<GameObject>();
    10.  
    11.         foreach (Collider hit in checkObj)
    12.         {
    13.             Debug.Log("hit : " + hit.name);
    14.             RaycastHit rayHit;
    15.                 if (hit.tag != "Floor")
    16.                 {
    17.                     this.gameObject.transform.SetParent(hit.gameObject.transform,true);
    18.                 }
    19.                 else if (hit.tag == "Floor")
    20.                 {
    21.                     stuckToObject = true;
    22.                 }
    23.                 break;
    24.         }

    This works fine if the scale of the objects are 1,1,1 or 100,100,100.

    But when the scale of the object is different, it stretches the sticky bomb creating weird shapes?

    I thought the line below made the object keep its relative scale, transform and rotation?

    Code (CSharp):
    1. this.gameObject.transform.SetParent(hit.gameObject.transform,true);;
    Is there any way to stop an object from rescaling?

    Any help would be great, thanks.
     
  2. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    Personally i just use the "parent" property and never had any issues with it.
    The SetParent Method - in my books - is only needed if you want to parent UI elements (Rect Transforms)

    Code (CSharp):
    1. child.transform.parent = this.transform;
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    When you say different, do you mean non-uniform? Like 1,3,2 or something? If so, then no, it will be distorted.
     
  4. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309

    This still makes the object scale weirdly :/
     
  5. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Yeah exactly what i mean, and why is that a problem? or how could i counteract it?
     
  6. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    When I've had similar problems, I always found that the best (though not always quickest) solution was to rework where my non-identity scaling was in the hierarchy. I find that it's best for such scaling to be as far down into the hierarchy as possible.

    In your case, is the parent scaled because it has a mesh/sprite/collider attached? If so, I'd recommend creating a child under the parent, moving the mesh/sprite/collider to the child object, and scaling the child instead of the parent. The parent can keep any non-spatial components that don't need to be on the same object as those components that got moved to the child. That way, any additional children added to the parent can have their own distinct scaling, and don't get messed up by the scaling of the parent or any of the other children.
     
    zombiegorilla likes this.
  7. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309

    I just attempted this, it works but i would have to do this for over 300 models in the scene :(