How to do it...

To make a character draw or sheath a weapon, follow these steps:

  1. Import the Warrior character with the Idle and Draw animations into Unity.
  2. Import a Sword prop to Unity.
  3. Go to the Import Settings | Animation tab. Select the Draw animation and navigate to the Events section.
  4. Add a Draw event in the frame where the character's hand reaches the sword in the sheath.
  5. Create an Animator Controller with the Idle and Draw animations.
  6. Create a Draw Trigger parameter in the controller.
  7. Create two transitions:
    • IdleDraw with one condition:Draw Trigger parameter. Has Exit Time should be set to false and Transition Duration set to around 0.2 seconds.
    • DrawIdle with no conditions: Has Exit Time should be set to true and Transition Duration set to around 0.2 seconds.
  8. Place the Warrior character in the scene.
  9. Assign the controller to the Warrior game object's Animator component.
  10. Place the Sword prop in the scene. Move it near the character's hips and pose it as it needs to be when it lays in the sheath.
  11. When you are happy with the Sword's position, create an empty child object in it. Name it SheathMarker and drag it onto the hip bone of the character to parent it to Warrior's hips. This way the SheathMarker will store our Sword's initial position and rotation relative to our character's hips. You should also parent the Sword to SheathMarker. See the following screenshot for reference:
Sheath Marker (red) stores sword's desired position and rotation
  1. Move the Sword to the Warrior's hand. Pose the sword as desired. Create another empty child object in the Sword and name it HandMarker. Drag and drop the HandMarker game object onto the right-hand bone in the character's rig to parent it to the Warrior's hand. See the following screenshot for reference:
Hand Marker (orange) stores sword's desired position and rotation in the hand
  1. Write a new script and call it DrawWeapon.cs. In this script's Update() function, we check if player pressed the space bar. If so, we first set the Draw Trigger parameter in the controller to play the animation:
        if (Input.GetKeyDown(KeyCode.Space)) 
        { 
            anim.SetTrigger("Draw"); 
        } 
  1. We also interpolate the Sword's position and rotation to its parent game object's position and rotation (Hand Marker or Sheath Marker):
        if (weapon.parent != null) 
        { 
            if ((weapon.position -     
        weapon.parent.position).sqrMagnitude > 0.0001f) 
            { 
                weapon.position = 
                Vector3.Lerp(weapon.position, 
                weapon.parent.position, Time.deltaTime * 
                lerpSpeed); 
         
                weapon.rotation = 
                Quaternion.Lerp(weapon.rotation, 
                weapon.parent.rotation, Time.deltaTime * 
                lerpSpeed); 
            } 
        } 
  1. In the preceding script, the weapon variable holds the reference to Sword's Transform component. The public float lerpSpeed variable is set in the Inspector.
  2. This script has also a public voidDraw() function, which is called by an Animation Event set in the Draw animation. In this function, we check if the sword is already in hand with the public bool weaponInHand variable. We need to set this variable's initial value in the Inspector, depending on the initial Sword position (in hand or in sheath). The Draw() function only changes the Sword parent and sets the weaponInHand flag:
        if (weaponInHand) 
        { 
            weaponInHand = false; 
            weapon.parent = sheathMarker; 
        } 
        else 
        { 
            weaponInHand = true; 
            weapon.parent = handMarker; 
        } 
  1. Attach the script to the character. Drag and drop the Sheath Marker game object to the Sheath Marker field in the script, the Sword game object to the Weapon field, and the Hand Marker to the Hand Marker field. Set the Weapon In Hand checkbox to match the Sword's initial position.
  2. Play the game and press the space bar to see the effect.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.170.187