Search Unity

Attach gameobject to avatar bone

Discussion in 'Scripting' started by zachypin, Jun 13, 2011.

  1. zachypin

    zachypin

    Joined:
    Jun 7, 2011
    Posts:
    33
    Hi,

    I've looked through the unity answers and although some of the questions in reference to this help, none of them seem to hit exactly what I need. I have a gameobject that I currently want to attach to the head bone of the character to be a hat. I would just attach it in the hierarchy and render visible in some cases, but the problem is that my character isn't in the hierarchy until the game is started. There is a selection screen where the user gets to choose which avatar they want to use. Because of this I'm not sure how to tell an object to find another object, then find the specific bone it needs to attach to.

    Thanks!

    link to same question answers http://answers.unity3d.com/question...3&viewedQuestions=14769&viewedQuestions=51929
     
    Last edited: Jun 13, 2011
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    You can use tag or name to find an object whenever its present.

    (ie)

    if(gameObject.FindWithTag("HeadBone")) {
    var myHead = gameObject.FindWithTag("HeadBone");//Assign the bone when found
    myHat.transform.parent = myHead.gameObject.transform;//Make my head the parent of my hat
    myHat.transform.localPosition = Vector3(0, 0, 0);//Center it to the center of the bone or any offset
    myHat.transform.localRotation = Quaternion.identity;//line up with the rotation of the parent.
    }

    Something like this may do the trick.
    Ofc you would want to wrap this with a condition so that
    the hat will not equip the second the chaarcter is spawned.

    PS if your hat is a rigidbody then make the IsKinematic false, when equipped.
     
  3. zachypin

    zachypin

    Joined:
    Jun 7, 2011
    Posts:
    33
    My problem is that I can't attach, or rather, don't know how to attach a tag to the head. The reason is because it isn't ever in the project view, and it isn't in the hierarchy until I've pressed play and chosen my character. I tried attaching a tag at this point, but upon replay the tag doesn't stick.
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    If the head bone name never changes, you can just call it, get it's transform and set the parent of your hat to be that transform...

    Code (csharp):
    1.  
    2. public var hat : Transform;
    3.  
    4. function Start () {
    5. hat.parent = gameObject.Find("HeadBone").transform; // Attach hat to head bone.
    6. hat.localPosition = Vector3(0, 0, 0); // Set local position so that hat sits on top of the head.
    7. hat.localRotation = Quaternion.Identity; // Zero hat's rotation.
    8. }
    9.  
     
  5. zachypin

    zachypin

    Joined:
    Jun 7, 2011
    Posts:
    33
    Perfect. Thank you!
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    This would do great, if there was only one character though, but if you have multiple characters (multiple "HeadBone"'s), you would want to reconsider how you do it.

    For the case of your character, I would make a script on the base of it where you can both find your headbone when it starts and then keep track of what hat it has.

    Here is some example code of finding assigning a hat in the editor, and an example of replacing the hat during game play by either a prefab or existing game object.

    Code (csharp):
    1.  
    2. var hat : GameObject;
    3.  
    4. private var headBone : Transform;
    5. private var rHandBone : Transform;
    6. private var activeHat : GameObject;
    7.  
    8. function Start(){
    9.     for(var obj : Transform in gameObject.GetComponentsInChildren(Transform)){
    10.         if(obj.name=="HeadBone")headBone=obj;
    11.         if(obj.name=="RightHandBone")rHandBone=obj;
    12.     }
    13.    
    14.     if(hat  headBone){
    15.         activeHat=Instantiate(hat, headBone.position, headBone.rotation);
    16.         activeHat.transform.parent=headBone;
    17.     }
    18. }
    19.  
    20. function AddHatFromPrefab(obj : GameObject){
    21.     if(activeHat) Destroy(activeHat);
    22.     activeHat=Instantiate(obj, headBone.position, headBone.rotation);
    23.     activeHat.transform.parent=headBone;
    24. }
    25.  
    26. function AddHatFromScene(obj : GameObject){
    27.     if(activeHat) Destroy(activeHat);
    28.     activeHat=obj;
    29.     activeHat.transform.position=headBone.position;
    30.     activeHat.transform.rotation=headBone.rotation;
    31.     activeHat.transform.parent=headBone;
    32. }
    33.  
     
    sanchez_x likes this.