网格寻路组件

 public class L51 : MonoBehaviour
{
    public NavMeshAgent agent;
    // Start is called before the first frame update
    void Start()
    {
        //Nav Mesh Agent 相关参数
        //Steering 移动设置,寻路时的各种最大速度
        //Stopping Distance 当靠近目标多少距离时,停止运动
        //Auto Braking 自动减速,如果存在连续移动,不建议启用
        //Obstacle Avoidance 避障设置
        //Radius 用于计算障碍物和其他寻路对象之间的碰撞
        //Height  通过头顶障碍物时用于计算高度间隙使用
        //Priority 避障时,数字较小的障碍物表示较高的优先级,优先级低的会忽略避障

        //Path Finding  路径寻找规则
        //Auto Traverse Off Mesh Link 自动遍历网格外的其他网格连接,如果要自定义判断就要关闭该功能
        //Auto Repath 自动重设路线,到达路径后段时会再次尝试寻路,当没有到达目标的路径时,会生成一条到达与目标位置最近的可达点路径
        //Area Mask 寻路时考虑的区域,如果不想考虑某些区域则取消选中

        //代码相关
        //设置目标点
        //agent.SetDestination()

        //停止寻路
        //agent.isStopped = true;

        //是否找到路径
        if (agent.hasPath)
        {

        }

        //得到目标点
        print(agent.destination);
        //得到当前路径
        print(agent.path);
        //路径是否在计算中
        if (agent.pathPending)
        {

        }
        //得到路径状态
        print(agent.pathStatus);
        //是否更新位置
        agent.updatePosition = true;
        //是否更新旋转
        agent.updateRotation = true;
        //代理速度
        print(agent.velocity);

        //手动寻路
        //计算生成路径
        NavMeshPath path = new NavMeshPath();
        if (agent.CalculatePath(Vector3.zero,path))
        {

        }
        //设置新路径
        if (agent.SetPath(path))
        {

        }
        //清除路径
        agent.ResetPath();
        //调整到指定点位置
        agent.Warp(Vector3.zero);

    }
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit))
            {
                agent.SetDestination(hit.point);
            }
        }
    }
}
posted @ 2025-04-09 18:28  cannedmint  阅读(28)  评论(0)    收藏  举报