首页 > 解决方案 > 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();
    }
}

非常感谢任何帮助:)

标签: c#unity3dartificial-intelligence

解决方案


尝试将 SetDestination(Vector3) 替换为 SetDestination(transform.position) 并将转换链接到您尝试跟随的对象的子游戏对象。

脚本也可能会被你的虚无和统一的虚无所迷惑。尝试重命名函数。


推荐阅读