Search Unity

Parenting through scripting

Discussion in 'Scripting' started by Apprenticeneauxs, May 4, 2010.

  1. Apprenticeneauxs

    Apprenticeneauxs

    Joined:
    Mar 21, 2009
    Posts:
    256
    How can I attach a sword to the player's hand? I have a sword with a joint in it... the player needs to be able to put the sword into a sheath on his back.. then on key press take sword out again. Is this possible through scripting? The rotations would have to match the joint in hand and sheath so the sword would be properly oriented. Please advise...
     
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    normally, that would be done through animation rather than scripting. Animate your character in a 3d package like maya or 3ds or use unity's own animation tools and then just trigger the animation through script.

    on a separate note, to parent something in unity, use : transform.parent
     
  3. Apprenticeneauxs

    Apprenticeneauxs

    Joined:
    Mar 21, 2009
    Posts:
    256
    Ok so is it possible to do this through code or does it have to be done via the editor? Seems like you should be able to parent the weapon from bone to bone with code.
     
  4. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    No it's not done via animation "normally". You just have to attach the sword to your hand bone. And the code used for this is;

    Code (csharp):
    1. transform.parent = GameObject.Find("/Character/Arm/Hand/").transform;
    Use your character's bone order when finding the one to attach the sword.
     
  5. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    In trying to understand this, the code snippet you are using is for the hand, and that is the parent but what is the code snippet for the weapon, I mean how do you attach the weapon of do you put a script on the weapon with this portion of the code?
    I thought it would be done with animation also and reason being is because this character has a weapon that is on his back at all times and when you make run with the sword in his hand it fades away from his hand when he stops and then pops back into hand at a stand still, so I thought that was an animation thing but how would you do that in code?
     
  6. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    That code snippet is for the Weapon.

    The Parenting system is based in the transform. So when you set an objects transform.parent it parents the object to whatever transform it is assigned, in this case the hand.

    In short if you take the weapon's transform.parent attribute and assign it to the hand's transform it will be parented.
     
  7. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    I think that I understand the code portion but it doesn't work correctly because every time I stop the sword moves out of the hand and then back into the hand when he completely stops? Any suggestions.
    Code (csharp):
    1.  
    2.         GameObject sword = new GameObject("Sword1");
    3.         sword.transform.parent = GameObject.Find("MasterMover/Root/Spine1/Spine2/LtClav/LtShoulder/LtElbow/LtWrist/LtPalm/LtHand1/LtHand2").transform;
    4.  
    I know there is a lot working there but this is how darquelord rigged and animated the character so these are the names for the connection path I need to get to the place holder for the sword.
     
  8. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The method is basically OK, but you need to position the sword in the hand's local coordinate space. You can set this from the script with transform.localPosition but determining the exact position is generally just a matter of tweaking until it looks right.