preface

Navigation enables objects to move intelligently to a certain position, which can be used in the game to complete the intelligent path of some NPCS and the pathfinding function of the protagonist

1. Render the scene

The rendering of the scene can tell the character where to go and where the obstacles are, so you can navigate the scene and avoid the character hitting the wall

Set the objects to be baked in the scene to static:

Select the object to bake, then set it to static in the upper right corner of the Inspector to bake it



Open the NaNavigation Baking panel:

In Windows, find the AI and click on it.



Set baking in navigation:

Modify the Bake parameters to achieve the rendering effect, such as climbing stairs, jumping over gullies, jumping from heights, etc. Adjust the following parameters, which are shown in the diagram above in real time



– Meanings of parameters:

  • Agent Radius: Defines the distance between the mesh and the edge of the terrain
  • Agent Height: Defines the maximum Height that can be passed
  • Max Slope: Defines the maximum Slope at which a staircase can be climbed
  • Step Height: Defines the maximum Height at which a Step can be ascended (The maximum height must be less than the maximum passable height)
  • Drop Height: Maximum permissible Drop distance
  • Jump Distance: Maximum Jump Distance allowed

– Render the result as follows:

The light blue area is where the character can move



For the Settings of the other three options in Navigation

  • The Object option

    After selecting an Object in the scene, the options as shown in the figure will appear in the Object. The basic Settings are as shown in the figureAdjust the baking of objects so that the character can move or jump in the area

  • Areas panel

Areas are mainly used to set the weights of different Areas, so that you can ensure that the character moves in the first place (Jump is high by default, so when Object is set to Jump, it will Jump instead of going around in a big circle).

  • Agents panel

    This panel is used by the role to add the Nav Mesh Agent component

2. Dynamic obstacles that can be moved

Add Nav Mesh Obstacle module directly to the object, and dynamically moving obstacles can be formed by setting parameters:

Only when you select Carve, you can dynamically bake, and in this way, you can realize moving obstacles

3. NPC movement Settings

Add the Nav Mesh Agent component to the NPC

After adding components, set related parameters:

  • Steering (control)
  • Speed: Maximum movement Speed
  • Angular Speed: Maximum Angular Speed while traveling
  • Acceleration: The maximum Acceleration, which controls how fast the speed changes
  • Stopping Distance: the Distance to the destination is less than this value
  • Auto Braking: To check, to stop motion after reaching the target point, without buffering motion

Commonly used API:

  • ActivateCurrentOffMeshLink: activate or current off – MeshLink is prohibited.
  • CalculatePath: Calculates the path to a point and saves it
  • CompleteOffMeshLink: Completes the move of the current offMeshLink
  • Move: Moves to a point relative to the current position
  • ResetPath: clears the current path
  • SetDestination: sets the target point
  • SetPath: Sets a path
  • Warp: Teleports to a point
  • RemainingDistance: distance to target point
  • DesiredVelocity: desiredVelocity. The direction is the direction of the shortest path to the target point

Write code to control the movement of objects

Implement a series of objects for the character to pass through:

 	public GameObject[] pos; // Define an array of points
 	private NavMeshAgent nav; // Define an NPC navigation component (be sure to add the unityEngine. AI class in the header file)
 	private int nub = 0;   // Define an integer
Copy the code

Get the object NavMeshAgent component:

void Start()
    {
        nav = GetComponent<NavMeshAgent>();
    }
Copy the code

Collision detection is performed, and if the object reaches the corresponding object, the array moves to the next:

    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag=="object")    nub++;
    }
Copy the code

Finally, to control the navigation of the object to the specified point, call the DESTINATION API:

    void Update()
    {
        if (Vector3.Distance(transform.position, pos[nub].transform.position) >= 0.1 f) { nav.destination = pos[nub].transform.position; }}Copy the code

conclusion

Using NavNavigation is a complicated process, but it will be easy to learn how to use it