栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

A*Pathfind Project的使用

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

A*Pathfind Project的使用

本文主要是对该文章的改进:Unity学习笔记--易学易会的unity中A星寻路插件:A*Pathfind Project的使用_秋瞑小雁客的博客-CSDN博客_unity 寻路插件

上述文章中的代码在运行过程中,角色到达目标点后,会出现数组越界的情况 ​

 针对这个问题,我们定位在代码区的第51行

 Vector3 dir = (path.vectorPath[currentWaypoint + 1] - transform.position);

我认为主要是 + 1 引起的数组越界

解决方法如下:

1.声明一个变量,作为判断使用

int flag = 1;

2.在45-49行的代码中加入 flag++

if (currentWaypoint >= path.vectorPath.Count)
        {
            Debug.Log("路径搜索结束");
            flag++;          
            return;
        }

 3.对这些代码进行判断

 修改后如下

if ((currentWaypoint + 1) <= flag)
        {
            Vector3 dir = (path.vectorPath[currentWaypoint + 1] - transform.position);//.normalized;
            dir *= speed * Time.fixedDeltaTime;
                        
            //玩家转向
            transform.Translate(Vector3.forward * Time.fixedDeltaTime * speed);
            Quaternion targetRotation = Quaternion.LookRotation(dir);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * turnSpeed);//(起始方向,终止方向,速度)
            //玩家当前位置与当前的航向点距离小于一个给定值后,转向下一个航向点
            if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance)
            {
                currentWaypoint++;
                return;
            }           
        }       

对此,成功解决数组越界问题

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901687.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号