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

Parenting GameObject to another animated GameObject

Discussion in 'Scripting' started by Tafk, Apr 20, 2016.

  1. Tafk

    Tafk

    Joined:
    Feb 5, 2013
    Posts:
    20
    What's the best way to parent GameObject to anoter animted GameObject? It seems like Unity locks all transforms in the hierarchy of the animated object. I'll give you an example of what I'm talking about:
    1) Parent object A to animated object B via SetParent(Transform). Everything is ok, object A keeps its global scale
    2) On LateUpdate() parent object A to another non-animted object. Expected behaviour - object A keeps its global scale, real behaviour - object A keeps its localScale therefore modifying its global scale and in my case becomes smaller and smaller with each reparenting.

    Thanks in advance.
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    If your issue is scale then the only advice I have is to leave your object scale as 1 as much as possible.

    If your issue is parenting an object to an animated object, it may depend on whether it was from an FBX or from mechanim, although I imagine in both cases you'd probably have to expose the bone objects and parent your target object to the animated target bone.

    What exactly are you trying to achieve though?
     
  3. Tafk

    Tafk

    Joined:
    Feb 5, 2013
    Posts:
    20
    Hi, thanks for response.

    It's exactly what I'm trying to achieve - parent an object to the animated bone. It works fine, but whenever I try to detach said object from animated bone via SetParent(Transform) it keeps its localScale resulting in different global scale each time I do it.

    To be more specific:
    We have an animated character who has a slot in his hand for different kinds of objects, whenever collision is registered I attach incoming object to the bone via SetParent(Transform). You can remove attached object by dragging it, whenever this happens I remove object from character hand by setting it transform to root of our scene, but it results in object keeping it's local scale. It works fine with animator disabled.

    edit: btw, our object is animated via mechanim.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    SetParent takes an optional second parameter. Try setting it to true or false. One of them works.
     
  5. Tafk

    Tafk

    Joined:
    Feb 5, 2013
    Posts:
    20
    SetParent(Transform, bool) second parameter determinates should object keep its global position, rotation and scale. Default value of this parameter is true and it's exactly what I need.