Unity3D easy-to-understand-navigation mesh automatic path finding (Navigation Mesh)

NavMesh (navigation mesh) is a technology used to realize automatic pathfinding of dynamic objects in the 3D game world. It simplifies the complex structure and organization relationship in the game into a grid with certain information, and passes on these grids. A series of calculations to realize automatic path finding. . When navigating, you only need to mount a navigation component to the navigation object, and the navigation object will find the most direct route based on the target point and follow the route to the target point.

Let's introduce the application of NavMesh through a simple Sample:

1. Create three Cubes in the Scene and place them as shown below.

2. Select the three Cubes in the above figure, and select Navigation Static which is the static drop-down option in the Inspector panel, as shown in the figure below.

3. Select Windows-Navigation in the menu bar in turn, and open the rear panel as follows.

Click the Bake button in the lower right corner of the panel to generate the navigation grid. The following figure shows the generated navigation grid.

4. The following can make a moving body move to the target position according to a navigation grid.

First create a new Cube as the target location and name it TargetCube. Then create a capsule (capsule) motion body, hang a Nav Mesh Agent (Component-Navigation-Nav Mesh Agent) for the capsule; finally write a script to realize automatic path finding. The script is as follows:

Copy code

using UnityEngine;
using System.Collections;

public class Run : MonoBehaviour {

    public Transform TargetObject = null;
 
    void Start () {
        if (TargetObject != null)
        {
            GetComponent<NavMeshAgent>().destination = TargetObject.position;
        }
    } 
    void Update () {
    
    }
}

Copy code

After the script is created, mount it on the capsule body, and then assign TargetCube to the Run script of the capsule body to run the scene, as shown in the figure below, the capsule body will move to the Cube position in the direction of the arrow.

Such a simple automatic path finding is completed. If you want to find a more precise path, or to achieve uphill, drilling "bridge holes", etc., you can adjust it according to the relevant parameters described below.

The following describes the relevant parameters of the Navigation component and the Nav Mesh Agent component.

Navigation

  1. Object: Object parameter panel
    • Navigation Static: When checked, it means that the object participates in the baking of the navigation grid.
    • OffMeshLink Generation: After checking, you can jump navigation grid and drop.
  2. Bake: Baking parameter panel  
    • Radius: A representative object radius. The smaller the radius, the larger the grid area generated.
    • Height: The height of a representative object.
    • Max Slope: The slope of the slope.
    • Ste Height: The height of the steps.
    • Drop Height: The maximum drop distance allowed.
    • Jump Distance: Allow the maximum jumping distance.
    • Min Region Area: If the grid area is less than this value, no navigation grid will be generated.
    • Width Inaccuracy: Allow the error of the maximum width.
    • Height Inaccuracy: Allow the maximum height error.
    • Height Mesh: After checking, the height information will be saved, and some performance and storage space will be consumed.

Nav Mesh Agent: Navigation component parameter panel    

  • Radius: the radius of the object
  • Speed: the maximum speed of the object
  • Acceleration: the acceleration of the object
  • Augular Speed: The angular speed when turning during travel.
  • Stopping Distance: How far is the distance from the target to stop.
  • Auto Traverse Off Mesh Link: Whether to use the default method to pass the link path.
  • Auto Repath: Whether to restart the path finding after the travel is interrupted for some reasons.
  • Height: The height of the object.
  • Base Offset: The vertical offset between the collision model and the solid model.
  • Obstacle Avoidance Type: The performance registration of obstacle avoidance. The None option means not avoiding obstacles. The higher the level, the better the avoidance effect and the more performance it consumes.
  • Avoidance Priority: Avoidance priority.
  • NavMesh Walkable: The mesh layer mask that the object can travel through.

Guess you like

Origin blog.csdn.net/u011105442/article/details/108378142