AI 在跟随路径点时面向奇怪的方向 UNITY

我的问题是,每当它试图朝当前路径点移动时,它会面向奇怪的方向,并且不会完全按照路径行进。

IEnumerator FollowPath() {    Vector3 currentWaypoint = path[0];    while (true) {        //transform.LookAt (currentWaypoint);        if (transform.position == currentWaypoint) {            PathRequestManager.RequestPath(transform.position,target.position,OnPathFound);            targetIndex=0;            targetIndex ++;            //Debug.Log(currentWaypoint);            if (targetIndex >= path.Length) {                targetIndex =0;                path = new Vector3[0];                //yield break;            }            currentWaypoint = path[targetIndex];        }        transform.LookAt (currentWaypoint);        transform.position = Vector3.MoveTowards(transform.position,currentWaypoint,speed * Time.deltaTime);        yield return null;    }}  

目前使用的是A*路径查找算法。完整的源代码在这里可以找到: https://github.com/SebLague/Pathfinding

当它到达当前路径点时,它会随机改变方向或随机面向某个方向,并且不会继续前往下一个路径点。 (截图)


回答:

在代码的这一部分,targetIndex 总是会计算为 1

targetIndex=0;targetIndex ++;

所以基本上,它永远不会超过第一个路径点。

此外,我建议不要使用

if (transform.position == currentWaypoint)

而是使用以下方法:

float threshold = 0.1f;                // 根据需要调整if ((currentWaypoint - transform.position).sqrMagnitude < threshold)

这是因为我认为 MoveTowards 可能会超出目标或无法精确到达目标向量,并且在目标向量上徘徊时会剧烈改变方向。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注