c# - Unity NavMeshAgent 与玩家碰撞时开始转向不同的方向
问题描述
所以我有一个胶囊作为我的 NavMeshAgent(现在),它跟随玩家并避开墙壁,但是当玩家与胶囊碰撞时,胶囊开始随机游走。我的 NavMeshAgent 代码如下: [SerializeField] 转换目标;
NavMeshAgent navMeshAgent;
// Start is called before the first frame update
void FixedUpdate()
{
navMeshAgent = this.GetComponent<NavMeshAgent>();
if (navMeshAgent == null)
{
Debug.LogError("The nav mesh agent component is not attached to " + gameObject.name);
}
else if (navMeshAgent.enabled == true)
{
SetDestination();
}
}
private void SetDestination()
{
if (destination != null)
{
Vector3 targetVector = destination.transform.position;
navMeshAgent.SetDestination(targetVector);
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
SetDestination();
}
}
非常感谢任何帮助:)
解决方案
尝试将 SetDestination(Vector3) 替换为 SetDestination(transform.position) 并将转换链接到您尝试跟随的对象的子游戏对象。
脚本也可能会被你的虚无和统一的虚无所迷惑。尝试重命名函数。
推荐阅读
- c# - 如何将不同类型的参数传递给通用 ServiceFilter 或 ActionFilter
- python-3.x - Python 数组交集
- javascript - 如何在jquery中的内部html下动态加载路径
- docker - 如何在开发模式下将 jira 作为 docker 容器运行?
- node.js - Mongoose:自定义验证器的 isAsync 选项已弃用
- python - Python:尝试合并两个必须相加值的字典时出现问题
- reactjs - 在 react Typescript 中通过地图显示数组数据
- excel - 基于 Excel 工作表中数据的自动电子邮件
- javascript - 使用私有方法语法时,“func-names”eslint 规则失败
- sql - 一对多关系限制为 N 个