当AI到达目标时,翻译到固定位置

我正在尝试让AI代理站在实际目标旁的一个固定位置上。

我被要求分享代码。这是一个最终状态机的脚本,我是在网上找到的。

这是用C#编写的整个状态代码:

public class GoToSpecificPoint : IShopperState{private readonly StatePatternShopper shopper;private readonly float distanceFromShelfModifier = 1.5f;private int nextWayPoint;private bool enRoute = false;private bool waitingForPlayer = false;private float initialPlayerDistanceFromShelf = 1f;private Transform playerTransform;private Vector3 targetLocation;private bool inPlayerSpace = false;private bool alreadyPicked = false;public GoToSpecificPoint(StatePatternShopper statePatternShopper){    shopper = statePatternShopper;}public void UpdateState(){    if (PlayerStillAtShelf())    {        enRoute = false;        waitingForPlayer = true;    }    else if (waitingForPlayer && !PlayerStillAtShelf())    {        waitingForPlayer = false;        ToReachPointState();    }    }private bool PlayerStillAtShelf(){    float dist;    if ((dist = Vector3.Distance(targetLocation, playerTransform.position)) > (initialPlayerDistanceFromShelf * distanceFromShelfModifier))    {        return false;    }    return true;}public void SpecificPoint(Vector3 target, Transform player){    alreadyPicked = false;    enRoute = true;    playerTransform = player;    target = new Vector3(player.position.x, 0, player.position.z - 1);    targetLocation = target;    initialPlayerDistanceFromShelf = Vector3.Distance(targetLocation, playerTransform.position);    shopper.meshRendererFlag.material.color = Color.red;    shopper.navMeshAgent.destination = targetLocation;    shopper.navMeshAgent.Resume();    shopper.animator.SetBool("Walk", true);}

}

我希望“目标”真的靠近玩家,这样AI就能站在玩家旁的一个固定位置上。

编辑:场景是一个超市,目标是一个用户。用户正在接近货架,AI正在接近用户。所以我需要他们站在一条线上。


回答:

我从你的代码中可以看出,你在计算目标位置,但在下一行你没有将这个目标赋值给targetLocation。你做了以下操作:

target = new Vector3 (player.position.x, 0, player.position.z - 1);target = targetLocation; // 你覆盖了上面计算的目标

将这行改为:

targetLocation = target;

因为下面你是在赋值targetLocation。在这里:

shopper.navMeshAgent.destination = targetLocation;

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中创建了一个多类分类项目。该项目可以对…

发表回复

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