How to do it...

To create a smooth blend between walk and run animations, follow these steps:

  1. Import your animated character with Idle, Walk, and Run animations. Walk and Run animations should be done "in place".
  2. Set the animations to Loop Time in the character asset's Inspector in the Animation tab. If your character's rig is set to Humanoid, you may also need to set all the Bake Into Pose options to true for each animation and all the Based Upon options to Original, as shown in the following screenshot:
Settings of in place Idle, Walk and Run animations
  1. Create an Animator Controller for your character.
  2. Drag and drop the Idle animation into the Animator Controller to make it the default state.
  3. Add a float Speed parameter in the Animator Controller.
  4. Right-click on the empty space in the Animator Controller and choose Create State | New From Blend Tree. This will create an empty Blend Tree.
  5. Click on the Blend Tree and change its name in the Inspector to something meaningful; we use WalkAndRunBlend in this example.
  6. Double-click on the Blend Tree. It will open the Blend Tree settings.
  7. Click on the plus button (marked with number 1 in the following screenshot) and choose the Add Motion Field option twice. Two Motion fields will be added.
  1. Drag and drop your WalkInPlace animation in the first (upper) field and your RunInPlace animation in the second (lower) field. Fields are marked with number 2, as shown in the following screenshot:
Blend Tree properties
  1. Unselect the Automate Thresholds option (3 in the preceding screenshot). This will make the Threshold field available for each Motion field. These are the thresholds of the Parameter used by the Blend Tree (marked with 4 in the preceding screenshot). If the Parameter is set to the exact value of a given threshold, the animation corresponding to that threshold is played. If the Parameter is set to a value between two thresholds, both animations are played with appropriate weights (and are blended together).
  2. Set the Thresholds to 1 for the WalkInPlace motion and 4 for the RunInPlaceMotion (you may need to adjust these values later).
  3. Click on the Play button in the Preview window, below the Blend Tree settings.
  4. Find the Speed parameter slider on the Blend Tree node, and move it to see the animations blending smoothly, depending on the Speed parameter value.
  5. Double-click on the empty space in the Animator Controller to get out of the Blend Tree settings.
  6. Create two transitions:
  • Idle | WalkAndRunBlend with the condition set to Speed parameter greater than 0.5, Has Exit Time set to false, and Transition Duration set to 0.2 seconds.
  • WalkAndRunBlend | Idle with the condition set to Speed parameter less than 0.5, HasExitTime set to false, and TransitionDuration set to 0.2 seconds.
  1. Write a script to move your character and set the Speed parameter value according to the movement speed of your character. We are using point and click movement in this example. You can find two scripts in the provided Unity project, ClickToMove.cs in the Shared scripts directory and SetSpeedFromAgent.cs in the Scripts directory of this recipe. The first one is used to set the destination for the Nav Mesh Agent component attached to the character.
  2. In the Update() function of the SetSpeedFromAgent.cs script, we use the desiredVelocity of the NavMeshAgent to set the Speed parameter in our Animator Controller:
        anim.SetFloat("Speed", agent.desiredVelocity.magnitude,
        0.2f, Time.deltaTime);
  1. The anim variable stores the reference to the Animator component of the character and the agent variable stores the reference to the Nav Mesh Agent component.
  2. We are also adjusting the agent.speed variable in the Update() function to set the maximum speed of the NavMeshAgent. This allows us to make the character run when player holds the Shift button and walk when player releases the Shift button:
        if (Input.GetKey(KeyCode.LeftShift) || 
        Input.GetKey(KeyCode.RightShift)) 

        { 

             agent.speed = 4f; 
        } 
        else 
        { 
             agent.speed = 1f; 
        }
..................Content has been hidden....................

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