unity-自动寻路功能实现

准备工作:

1、把所有场景物体都选中inspector面板右上角的static。

2、菜单栏Window-ai打开窗口,选择bake标签-点击下方bake按钮得到可行走区域,可调整radius半径,slope斜坡,step height楼梯高度。

3、给玩家角色添加组件nav mesh agent。

将以下脚本挂接到玩家角色上:

private NavMeshAgent agent;
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))//按下鼠标左键
        {
            RaycastHit hit;//记录射线碰到的物体的点坐标
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
            //从摄像机向屏幕上鼠标所点的位置发出射线,将射线查询结果放入hit,射线查询的距离是100单位
            {
                agent.destination = hit.point;//把射线碰到的物体的点坐标作为自动寻路的目标
            }

        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_47356957/article/details/131623159
今日推荐