How to do it...

To make the character jump, follow these steps:

  1. Import the character and repeat all the steps from the Using root motion to steer a character, Using animations for better looking transitions, and Using root motion for a 180 degrees turn recipes to have a character that is able to move properly.
  2. In the Animation Import Settings of the character, find your additional Jump, InAir, and Land animations. You may need to add them as new clips by clicking on the plus button in the Clips section (Animations tab) and choosing the appropriate animation as the source.
  3. Check all the Bake Into Pose options for in Jump, InAir, and Land animations.
  4. Open the Animator Controller (this should be the same controller as in the previous recipe) and add two parameters: a Trigger called Jump and a bool called OnGround.
  5. Drag and drop your Jump, InAir, and Land animations into the Animator Controller.
  6. Create these transitions, as shown in the following screenshot:
    • Any State | Jump with a Jump trigger parameter. Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds.
    • Jump | InAir with no conditions: Has Exit Time should be set to true and Transition Duration set to around 0.1 seconds.
    • InAir | Idle with one condition: OnGround parameter set to true.Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds.
  1. Create a new script and call it Jump.cs (you can find it in the Scripts directory of this recipe).
  2. In the Update() function of the script, we check if player pressed the space bar and if the character is on the ground: the onGround variable is set by the GroundCheck() function. If the player presses space bar and the character is on ground, we save the current game time as our lastJumpTime (this variable is used in the GroundCheck() function). Then we disable root motion on the Animator component by setting the anim.applyRootMotion variable to false. We set the onGround variable to false because our character is jumping. We add force to the Rigidbody component of the character and set the Trigger Jump in our Animator Controller. Lastly, we call the GroundCheck() function, which will be described later:
        if (Input.GetKeyDown(KeyCode.Space) && onGround) 
        { 
           lastJumpTime = Time.time; 
           anim.applyRootMotion = false; 
           onGround = false; 
           rb.AddForce(Vector3.up* jumpForceUp +
            transform.forward * jumpForceForward, ForceMode.Impulse); 
           anim.SetTrigger("Jump"); 
        } 
        GroundCheck(); 
  1. The anim variable stores the reference to the Animator component. The rb variable stores the reference to the Rigidbody component. Both variables are set in the Start() function. The jumpForceUp and jumpForceForward are public float variables that control the applied force magnitude.
  1. In the GroundCheck() function, we check if the current game time is bigger than our lastJumpTime plus the value of the public float groundCheckPauseTime variable. It this is not true, we are setting the onGround variable to false (we are assuming that the character is always in the air for a short moment after the player presses the jump button). Then we use the Physics.Raycast() method to check if our character stands on the ground. This method casts a ray from the groundCheck transform down. We set the maximum ray cast length with a public float variable, maxGroundCheckDistance. If the character is standing on ground, we set the onGround variable to true and we enable the root motion in the Animator component. We also set the OnGround parameter in our Animator Controller to the value of the onGround variable:
          void GroundCheck() 
        { 
         if(Time.time > lastJumpTime + groundCheckPauseTime      &&            
Physics.Raycast(groundCheck.position, Vector3.down, maxGroundCheckDistance)) { onGround = true; anim.applyRootMotion = true; } else { onGround = false; } anim.SetBool("OnGround", onGround); }
  1. Assign the script to our character.
  2. Create an empty child object of the character and call it GroundCheck. Set its local position to X = 0, Y = 0.1, and Z = 0. Make sure it is a child object of the character game object.
  3. Assign the GroundCheck game object to the Ground Check field of the Jump script attached to the character.
  4. Play the game and press the space bar to see the result.
..................Content has been hidden....................

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