Search Unity

Rigging for Sword action

Discussion in 'Formats & External Tools' started by Apprenticeneauxs, Jan 19, 2010.

  1. Apprenticeneauxs

    Apprenticeneauxs

    Joined:
    Mar 21, 2009
    Posts:
    256
    Ok I have a little character that carries a sword on s back. Now the sheath is part of the body and the sword is of course seperate. So the character will move normally and the sword will stay in the sheath until he needs to fight. I then need to take it from sheath and have it stay with the hand for animations. Then when your done it needs to be put away to perform other actions. So Im using Maya and im wondering if it is easier to set up a rig for this or just use a script for mounting it to sheath and hand at appropriate times. Any advice? If you understand rigging in Maya maybe you have the answer? Lemme know! Thanks!
     
  2. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,094
    well you can maybe set up that all in maya or use script in unity,

    in maya you can for exemple place a null where your sword handle his, and using a custom expression switch where the null will constrained to (hand of your character or on his back), then you can animate that switch in your channel box in maya.

    In unity maybe you can use similar process (you will still a null or empty object to set where the sword should be handle i guess)
    using unity 2.6.1 in the animation editor you could at the frame you nedd your sword be constrained to teh hand of your character, create an event >> using animation editor in unity and in your script you get trace of tat event to re-parent your sword in the hand of your character,

    same for doing opposite , mean get back the sword on his back.
     
  3. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Right.

    I deal with this sort of thing all the time, but in Max, so I'm slowly figuring out ways of making it easy for myself. What I'm thinking today is to make the character and the sword in Max (Maya for you) so that it is linked to the hand and it works. Then when everything looks good, make a dummy (called a 'null' in Maya?) and put it in the hand. Then clone it so you have two copies of the exact object with the exact same rotation, etc. Make one a child of the hand, and the other the parent of the sword. Unlink the whole sword object and dummy from the character and export them separately.

    In Unity, when you need to link the sword to the hand, you make the transform.position and .rotation of the Sword dummy object match that of the hand dummy object. Then it will be right where it was in Maya.

    Similar for the sheath on his back. In Maya, place the Sword exactly where it should be in the sheath, duplicate the sword's parent dummy, and make that a child of the sheath. That way, when you need to put the sword in the sheath, you just use Unity scripting to match their position and rotation.

    Make animations in Maya for sheathing / unsheathing and you're all set!
     
  4. Apprenticeneauxs

    Apprenticeneauxs

    Joined:
    Mar 21, 2009
    Posts:
    256
    Thanks guys. I set up attributes in Maya to toggle the visibility of two swords. One is always on his back and the other in his hand. So when I export to Unity of course it doesnt carry over this info. So now Im trying to figure out with a script how to get the visibility to toggle on and off based on a range of animation frames. Any Ideas?
     
  5. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Darque,

    Doing it the way you're talking about would be a combination of things like this to turn the sword visibility on and off:

    Code (csharp):
    1.     var sword= GameObject.Find("Sword");
    2.     sword.GetComponent(MeshRenderer).enabled = false;
    3.      // change to true to make it visible again
    4.  
    I suppose you'll have an animation for Unsheathing and Sheathing, and you'll want to toggle the visibility after the animation is finished. To see if an animation is finished playing, you'll need something like this. :

    Code (csharp):
    1.     if(!animation.IsPlaying("sheath")){
    2. // toggle visibility here
    3.     }
    4.  
    The other way to do it would be to have one sword that moves from the sheath to his hand by parenting to one or the other. To parent the sword to his hand you'll have something like this:

    Code (csharp):
    1.             var leftHand = GameObject.Find("Player/Bip01/Bip01 Pelvis/Bip01 Spine/Bip01 Spine1/Bip01 Spine2/Bip01 Spine3/Bip01 Neck/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand");
    2.             sword.transform.parent = leftHand.transform;
    3.  
    But your way is fine, so go with it! :D
     
  6. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Let's see if I can put that into a complete function for you. You'll need variables like this to set up:

    Code (csharp):
    1. var sheathing:boolean = false; // 'sheathing' is true while he is actually in the act of putting the sword away.
    2. var swordInHand:boolean = true; // start with sword in hand
    3.  
    Then trigger this function when he puts his sword away, with a key click or whatever:

    Code (csharp):
    1. function Sheathing(){
    2.     animation.Play("sheath");
    3.     sheathing = true;
    4. }
    This goes in Update for this reason: when he goes to sheath the sword, you trigger the 'sheath' animation, but you can't change the visibility of the sword til the animation is done playing. So you need to use the 'sheathing' state to wait til that's done, THEN change visibility, like this:


    Code (csharp):
    1. function Update(){
    2.     if(sheathing){
    3.        // check to see if animation is finished playing:
    4.        if(!animation.IsPlaying("sheath")){
    5.           // find sword in hand and turn off:
    6.          var swordInHand = GameObject.Find("Player/Bip01/Bip01 Pelvis/Bip01 Spine/Bip01 Spine1/Bip01 Spine2/Bip01 Spine3/Bip01 Neck/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Sword");
    7.             swordInHand.GetComponent(MeshRenderer).enabled = false;
    8.         // Now find sword on back and turn ON:
    9.         var swordOnBack = GameObject.Find("Player/Bip01/Bip01 Pelvis/Bip01 Spine/Bip01 Spine1/Bip01 Spine2/Bip01 Spine3/Sheath/Sword");
    10.         swordOnBack.GetComponent(MeshRenderer).enabled = true;
    11.         swordInHand = false; // he's now unarmed.
    12.         sheathing = false; // he's finished sheathing
    13.     }
    14. }
    The boolean 'swordInHand' exists so your scripts will know if he's actually holding the sword. If he tries to attack, check "swordInHand" to see if he can.

    Let me know how this works!
     
  7. atons

    atons

    Joined:
    Feb 16, 2010
    Posts:
    1
    I have also a character that has a sword in his back but there's a two sword in his back.
     
  8. BethesdaBogdan

    BethesdaBogdan

    Joined:
    Mar 8, 2013
    Posts:
    2
    Give the exact script please I beg of you