How to do it...

To make a character dodge, follow these steps:

  1. Import the character into Unity and place it in a scene.
  2. If you want the character to get damage and play hit reactions, follow the previous recipe.
  3. Create a new Animator Controller or edit the one from the previous recipe.
  4. Drag and drop the Dodge animation into the controller.
  5. Create a Dodge Trigger parameter in the controller.
  6. Create two transitions:
    • SpiderIdleDodge with one condition: Dodge Trigger parameter. Has Exit Time should be set to false and Transition Duration set to 0.2 seconds.
    • DodgeSpiderIdle with no conditions: Has Exit Time should be set to true and Transition Duration set to 0.2 seconds.
  7. Add a Rigidbody component to the Spider character and freeze its rotations.
  8. Add a collider to the Spider character (we are using Sphere Collider for our Spider). You may also need to create a zero friction Physical Material and add it to the collider. It allows the animation to control Rigid Body movement without any additional friction.
  9. Set the Animator component's Update Mode to Animate Physics in our Spider.
  10. Attach the controller to the Animator component of our Spider.
  11. Create a new script and call it Dodge.cs. In this script's Update() function, we simply set the Dodge Trigger in the controller when player presses the space bar:
        if (Input.GetKeyDown(KeyCode.Space)) 

        { 

            anim.SetTrigger("Dodge"); 
        } 
  1. As always, the anim variable stores the reference to the Animator component and is set in the Start() function.
  2. Attach the script to the Spider character.
  3. Play the game and press the space bar to see the Spider play the dodge animation. It should be able to get out of the Humanoid character's attack range and avoid being hit.
..................Content has been hidden....................

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