How to do it...

To use root motion for moving and steering a character with Nav Mesh Agent component, follow these steps:

  1. Import the character with the Idle, WalkLeft, WalkForward, and WalkRight animations and set it up the same way as in the Using root motion to steer a character recipe, but don't write the RootMotionSteering.cs script as we will create a new one.
  2. Add the Nav Mesh Agent component to the character. Make sure it also has the Capsule Collider component and the Rigidbody component with frozen rotation in every axis. Also make sure that the Animator component's Apply Root Motion option is set to true and Update Mode set to Animate Physics (the same way as in the Using root motion to steer a character recipe).
  3. Bake the NavMesh in the scene. To do so, make sure your ground model has a collider attached (you can use the Mesh Collider component). Then go to Window | Navigation. Select the ground in your scene and set it to Navigation Static in the Object tab of the Navigation window. Click on the Bake button at the bottom of the Navigation window. After a short while, NavMesh should be baked (it is visible in the scene as a light blue, semitransparent mesh covering the ground model). It is needed for our NavMesh Agent to work.
  4. If your character is using any scripts from this book, remove them from its Inspector (we don't need the Jump, RootMotionSteering, and SetRawDirectionAndSpeed scripts).
  5. Create a new script and call it NavMeshAgentWithRigidBody.cs. We will use both the NavMesh Agent and the Rigidbody components in this script.
  1. In the Update() function of the script, we first disable updating rotation and position in the NavMesh Agent. That will prevent the agent from moving our character's transform. Then we calculate the float direction variable's value. We do it in a very similar way to what we did in the Using root motion to steer a character recipe, but instead of creating our own desiredMoveDirection vector, we are using the NavMesh Agent's desiredVelocity vector. This vector describes the speed and direction the agent would like to move with:
        agent.updatePosition = false; 
        agent.updateRotation = false; 
 
        direction = Vector3.Angle(transform.forward, 
        agent.desiredVelocity) * 
        Mathf.Sign(Vector3.Dot(agent.desiredVelocity, 
        transform.right)); 

In the preceding code agent is the variable in which we store the reference to the Nav Mesh Agent component. We set this reference in the Start() function.

  1. Next we calculate the float speed variable value: it's simply the magnitude of the agent.desiredVelocity vector. We also set the Direction, Speed, DirectionRaw, and SpeedRaw parameters in our Animator Controller using the anim variable that stores the reference to the Animator component. The anim variable is set in the Start() function. Lastly, we set the agent.nextPosition to be the same as our transform position every frame. This prevents the agent from moving away from our character's transform.
  2. Save the script and attach it to the character.
  3. We also need a script to tell the NavMesh Agent where we want to go. There is a ClickToMove.cs script in the Shared Scripts folder in the provided Unity example. In the Update() function, we use the agent.SetDestination() method when the player presses the left mouse button. A ray is cast from the mouse cursor position in the main camera's forward direction. If the ray hits a collider, the hit position is used to set the new destination for the NavMesh Agent:
        if (Input.GetKeyDown(KeyCode.Mouse0)) 
        { 
            if (Physics.Raycast(Camera.main.ScreenPointToRay(
            Input.mousePosition), out hit)) 
            { 
                agent.SetDestination(hit.point); 
            } 
        } 

In the preceding code agent is a public NavMeshAgent variable that stores the reference to the Nav Mesh Agent component. We assign this reference manually in the Inspector by dragging the game object with the Nav Mesh Agent component to the Agent field of the script. The hit variable is a global RaycastHit variable that is used by the Physics.Raycast() method to store the ray cast result.

  1. Save the script and attach it to the character. Drag and drop the character game object to the Agent field of the script (the script is attached to the same game object but can be attached to any game object).
  2. Play the game and click on the ground to see the character move using both NavMesh Agent and root motion.
..................Content has been hidden....................

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