[Unity2d] 2DNavMesh automatic pathfinding implementation

        In u3d, the system has its own NavMesh component, which can realize automatic pathfinding very conveniently. The new version of NavMesh has not been added to the engine function with the update of Unity. In the 2d project, we want to realize NavMesh automatic pathfinding. New components need to be downloaded. The URL is available here: GitHub - h8man/NavMeshPlus: Unity NavMesh 2D Pathfinding

        To use it in your projects:

        1. Unzip the downloaded compressed package (.zip), and copy the file to the Asset folder of the Unity project.
        2. Create an empty object in the scene root directory and name it NavMesh.
        3. Add the "Navigation Surface" component to the empty object, then add the NavMeshCollectSources2d component.
        4. Click "Rotate Surface to XY" to rotate the surface to XY (make the surface face the standard 2D camera x:-90;y:0;z:0) 5. Add the
        "Navigation Modifier" component to the scene object obstacle , covering the area.
        6. Click "Bake" in "Navigation Surface".

        The following are the specific steps:

        Step 1, create a TileMap (GameObject -> 2D Object -> Tilemap), make sure your Tilemap covers all walkable areas in the scene, add ground, obstacles, walls, etc.

        Step 2. Add a "Navigation Modifier" component to the ground, check Override Area, and set the Area Type property to Walkable. Similarly, add a "Navigation Modifier" component to obstacles or walls, check Override Area, and set the Area Type property to Not Walkable.

         Step 3. Create an empty object, name it NavMesh, add the "Navigation Surface" component to the empty object, and then add the NavMeshCollectSources2d component. Click "Rotate Surface to XY" to rotate the surface to XY (make the surface face the standard 2D camera x:-90;y:0;z:0), click "Bake" in "Navigation Surface".

         Step 4, add the "NavMeshAgent" component to the character. I encountered a problem in the implementation. After running, the Player object disappears (not destroyed). Solution: continue to add the "Agent Override 2d" component to the character, and the problem is solved. Next, we add a script to verify automatic pathfinding, the code is as follows:

using UnityEngine;
using UnityEngine.AI;

public class PlayerController : MonoBehaviour
{
    public Transform target;

    private NavMeshAgent _agent;

    private void Start()
    {
        _agent = GetComponent<NavMeshAgent>();
    }

    private void Update()
    {
        _agent.SetDestination(target.position);
    }
}

        The above code requires the friends to set a target point and run it.

        During the implementation process, the friends may report an error, "MissingComponentException: There is no 'MeshFilter' attached to the "Grid" game object, but a script is trying to access it. You probably need to add a MeshFilter to
the game object "Grid". Or your script needs to check if the component is attached before using it.", solution: add a MeshFilter component to Grid.

Guess you like

Origin blog.csdn.net/m0_51942776/article/details/131363259