Unity 6 AI导航网格中Agent无法移动或无故卡顿问题求助
我在NavMesh上生成小兵Agent,并将其目标设置为路径点数组中的点,但遇到了问题:非垂直移动时一切正常,一旦进入垂直移动状态,小兵有时会无故卡顿。
小兵生成代码
private void CreateMinion(PlayerNumber player, UnitSO unitData) { Transform[] pathPoints = GetCurrentPlayerPaths((int)player); Transform spawn = player == PlayerNumber.Player1 ? P1CreepsSpawn : P2CreepsSpawn; GameObject newMinion = Instantiate(unitPrefab, new Vector3(pathPoints[0].position.x, spawn.position.y), Quaternion.identity); newMinion.GetComponent<Unit>().minionAttributes = unitData; newMinion.GetComponent<Unit>().player = (int)player; newMinion.GetComponent<Unit>().playerPath = pathPoints; newMinion.GetComponent<NetworkObject>().Spawn(true); }
小兵OnNetworkSpawn逻辑
agent.updateRotation = false; agent.updateUpAxis = false; agent.speed = minionAttributes.MoveSpeed; if (NavMesh.SamplePosition(playerPath[pointIndex].position, out NavMeshHit hit, 0.25f, NavMesh.AllAreas)) { Debug.Log($"NAV HIT BABY: {hit.position}"); NavMeshPath path = new NavMeshPath(); bool yep = agent.CalculatePath(hit.position, path); Debug.Log($"Path: {path.status} ({yep})"); agent.SetDestination(hit.position); }
现象说明
- 垂直移动时:用
Debug.DrawLine绘制的红色路径显示异常,小兵出现卡顿 - 非垂直移动时:路径正常,小兵移动无问题
使用的是NavMesh Plus Components,如需更多细节可补充。
内容的提问来源于stack exchange,提问作者GaLzZy
相关产品推荐
相关产品推荐

