Search Unity

How to tell if a transform is under Mecanim control?

Discussion in 'Animation' started by JoeStrout, Dec 5, 2013.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm playing around with ragdolls and Mecanim (using this blog post as a starting point). At one point we need to move all the transforms in the model to where they landed, ragdoll-wise, so that the Mecanim engine can then smoothly interpolate to the next pose.

    The trouble is, if I have something that's not actually under Mecanim control — such as a sword that's just stuck into the hand of the player — it gets malpositioned by this process. If I skip such transforms, identified by name or tag or whatever, then it all works fine.

    But I'm looking for ways to make the script more foolproof, and not require so much setup. So that leads to my real question:

    Is there any way to tell, from code, whether a given transform is a Mecanim bone?

    It seems like this information must be in the avatar of the animator, but I couldn't find any useful methods or properties to get it out. Does anyone have a clue?

    Thanks,
    - Joe
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Dangit... as sometimes happens, pressing the Submit New Thread button causes my IQ to go up ten or twenty points.

    For the record, here's the answer (where 'c' is the transform we're curious about):

    Code (csharp):
    1.             bool found = false;
    2.             foreach (HumanBodyBones bone in System.Enum.GetValues(typeof(HumanBodyBones))) {
    3.                 if (anim.GetBoneTransform(bone) == c) {
    4.                     Debug.Log(c.gameObject.name + " is " + bone);
    5.                     found = true;
    6.                     break;
    7.                 }
    8.             }