Unity3d achieves enemy patrol through the Nav Mesh Agent component and solves the problem of "SetDestination" error reporting

First show the effect:

In the realization of the automatic patrol of the enemy, we can use the Nav Mesh Agent (navigation grid agent) that comes with unity, which can conveniently realize this function while saving the amount of redundant code.

First add the Nav Mesh Agent component to the enemy:

https://docs.unity.cn/cn/2018.4/Manual/class-NavMeshAgent.html

The above is the detailed introduction of this component in the official manual of unity. The more important ones here are Speed ​​(moving speed), Angular Speed ​​(rotation speed) and Stopping Distance (stopping when it is close to the target distance). These three values ​​​​need to be according to your own needs. settings, and the rest can be left as default.

Next on the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AIFindWay : MonoBehaviour
{
    public NavMeshAgent agent;
    public Transform[] Points;//通过数组形式可设置多个监测点
    int currentPoint;
    
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.SetDestination(Points[0].position);//通过Nav Mesh Agent组件的SetDestination 方法括号内为一个Vector3目标点
    }

    void Update()
    {
        if(agent.remainingDistance<agent.stoppingDistance)//到目标的剩余距离是否小于之前在 Inspector窗口中设置的停止距离
        {         
            currentPoint=(currentPoint+1)%Points.Length;//采用取余的方法实现敌人巡逻轨迹的循环
            agent.SetDestination(Points[currentPoint].position);
        }
    }
}

Add a script to the character and drag the Nev Mesh Agent into the Agent

And add two Point points, add two empty objects on the Hierarchy panel, and name them pos1, pos2, and label them for easy placement.

 

 There is also a need to pay attention here, the Y-axis values ​​of pos1 and pos2 should be as close as possible to the Y-axis of the ground, otherwise the characters will wander around during navigation. There may be doubts here, the enemy's mobile script? ? Through this component, there is no need to write movement scripts, and the speed and steering of the movement can be set in the component. Here, you only need to add an enemy with movement animation. When you run,

 "GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh!

This confusing mistake needs to introduce the concept of NavMesh's dynamic baking. First, store all the created terrain and environment objects (with collisions) in an empty object named Environment and perform the following operations at the same time:

Pull down Static in the Inspector and select Navigation Static:

 Next click Window->AI->Navigation and the following panel appears:

Agent Radius (the distance between the grid and the edge of the terrain)

Agent Height (the maximum height that can be passed)

Max Slope (the maximum slope that can be passed)

step Height (maximum gradient that can pass)

 Drop Height (allowed drop distance) Jump Height (allowed jump distance)

Generally, use the default value and click Bake to bake, and the following will appear:

 The enemy patrol function is completed, let's run it.

Guess you like

Origin blog.csdn.net/m0_64652083/article/details/128944308