【问题标题】:Unity NavMeshAgent Starts To Go a Different Direction When Collides With PlayerUnity NavMeshAgent 与玩家碰撞时开始转向不同的方向
【发布时间】:2020-04-21 01:56:52
【问题描述】:

所以我有一个胶囊作为我的 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# unity3d artificial-intelligence


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢你的帮助,我试试这个。
    • 他们仍然四处游荡,除非我误解了你的意思,将转换链接到我试图跟随的对象的子游戏对象。我所做的是将 Player 游戏对象放入一个空的游戏对象中,并将其设置为代理目的地。对吗?
    • 在 NavMesh 上试试这个教程:youtube.com/watch?v=CHV1ymlw-P8
    猜你喜欢
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多