You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多NavMesh Agent共存时部分Agent卡顿无法移动问题求助

核心逻辑代码

撤退触发方法:

public void StartRetreating()
{
    animator.SetBool("Attack", false);
    isRetreating = true;
    StartCoroutine(Retreat());
}

辅助方法与协程:

private Vector3 GetRandomPointAroundPlayer(Vector3 center, float radius)
{
    Vector3 randomDirection = Random.insideUnitSphere * radius;
    randomDirection.y = 0f;
    Vector3 randomPoint = center + randomDirection;

    NavMeshHit hit;
    if (NavMesh.SamplePosition(randomPoint, out hit, radius, NavMesh.AllAreas))
    {
        return hit.position;
    }

    // 未找到有效位置时返回原中心
    return center;
}

IEnumerator Retreat()
{
    yield return new WaitForSeconds(0.5f);
    animator.SetBool("Walk", false);
    wanderTarget = GetRandomPointAroundPlayer(enemySight.player.transform.position, wanderRadius);
    Collider[] colliders = Physics.OverlapSphere(wanderTarget, 3f); // 可调整半径
    foreach (Collider collider in colliders)
    {
        if (collider.CompareTag("RetreatPoint"))
        {
            // 附近有撤退点,重新获取随机目标
            wanderTarget = GetRandomPointAroundPlayer(enemySight.player.transform.position, wanderRadius);
        }
    }
    Walk(wanderTarget);
}

void Walk(Vector3 wanderTarget)
{
    if (canSpawnRetreatPoint)
    {
        retreatObject=Instantiate(retreatPoint, new(wanderTarget.x, -2, wanderTarget.z), Quaternion.identity);
        canSpawnRetreatPoint = false;
    }
    if (enemySight.playerOnRight == true && enemyWalk.facingRight == true)
    {
        enemyWalk.Flip();
    }
    else if (enemySight.playerOnRight == false && !enemyWalk.facingRight)
    {
        enemyWalk.Flip();
    }
    navMeshAgent.speed = enemyWalk.enemySpeed; // 同步NavMesh速度与敌人行走速度
    enemyWalk.enemyCurrentSpeed = navMeshAgent.velocity.sqrMagnitude;
    navMeshAgent.SetDestination(wanderTarget); // 设置移动目标
    navMeshAgent.updateRotation = false; // 防止敌人Sprite旋转
}

异常现象

  • 1-2个Agent时功能正常,增加到4个时,至少有一个Agent卡在行走/撤退状态:行走动画播放,但Agent无法移动。
  • 撤退点带有触发器,拖拽Agent到撤退点时,触发器正常触发状态切换(应向玩家移动),但Agent仍无法移动。
  • 两种卡顿场景下,NavMesh Agent均处于激活状态且目标位置正常,手动将Agent拖拽到玩家攻击点后,可恢复正常攻击与移动。

场景加载错误日志

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Graphs.Edge.WakeUp () (at <871096295abe423b9c0af38ec6fe4bf2>:0)
UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List`1[T] inEdges, System.Collections.Generic.List`1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <871096295abe423b9c0af38ec6fe4bf2>:0)
UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <871096295abe423b9c0af38ec6fe4bf2>:0)
UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <871096295abe423b9c0af38ec6fe4bf2>:0)
UnityEditor.Graphs.Graph.WakeUp () (at <871096295abe423b9c0af38ec6fe4bf2>:0)
UnityEditor.Graphs.Graph.OnEnable () (at <871096295abe423b9c0af38ec6fe4bf2>:0)

内容的提问来源于stack exchange,提问作者VChuckShunA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 05:16:39