Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug Group movement navigation works incorrect in build but it works correctly in editor

Discussion in 'Navigation' started by ParhamXTT, Mar 11, 2023.

  1. ParhamXTT

    ParhamXTT

    Joined:
    May 24, 2020
    Posts:
    28
    I have 1024 3D animated characters with Navmesh agent and Navigation script that updates Navmesh when new buildings , walls gates or towers are instanciated. (characters can go on the walls, stairs, some buildings, towers and gatehouses). when I run the game in the editor, over 10 times more characters move when I move them with command and all 1024 characters reach the destination in a shorter amount of time because they move together but when I build the game, characters move one by one and it takes over 10 times more time for all 1024 characters to reach their destination.
    my game is a medieval RTS and the script of runtime Navmesh generation is below :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using NavMeshBuilder = UnityEngine.AI.NavMeshBuilder;
    6.  
    7. // Build and update a localized navmesh from the sources marked by NavMeshSourceTag
    8. [DefaultExecutionOrder(-102)]
    9. public class LocalNavMeshBuilder : MonoBehaviour
    10. {
    11.     // The center of the build
    12.     public Transform m_Tracked;
    13.  
    14.     // The size of the build bounds
    15.     public Vector3 m_Size = new Vector3(80.0f, 20.0f, 80.0f);
    16.  
    17.     NavMeshData m_NavMesh;
    18.     AsyncOperation m_Operation;
    19.     NavMeshDataInstance m_Instance;
    20.     List<NavMeshBuildSource> m_Sources = new List<NavMeshBuildSource>();
    21.  
    22.     IEnumerator Start()
    23.     {
    24.         while (true)
    25.         {
    26.             UpdateNavMesh(true);
    27.             yield return m_Operation;
    28.         }
    29.     }
    30.  
    31.     void OnEnable()
    32.     {
    33.         // Construct and add navmesh
    34.         m_NavMesh = new NavMeshData();
    35.         m_Instance = NavMesh.AddNavMeshData(m_NavMesh);
    36.         if (m_Tracked == null)
    37.             m_Tracked = transform;
    38.         UpdateNavMesh(false);
    39.     }
    40.  
    41.     void OnDisable()
    42.     {
    43.         // Unload navmesh and clear handle
    44.         m_Instance.Remove();
    45.     }
    46.  
    47.     void UpdateNavMesh(bool asyncUpdate = false)
    48.     {
    49.         NavMeshSourceTag.Collect(ref m_Sources);
    50.         var defaultBuildSettings = NavMesh.GetSettingsByID(0);
    51.         var bounds = QuantizedBounds();
    52.  
    53.         if (asyncUpdate)
    54.             m_Operation = NavMeshBuilder.UpdateNavMeshDataAsync(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
    55.         else
    56.             NavMeshBuilder.UpdateNavMeshData(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
    57.     }
    58.  
    59.     static Vector3 Quantize(Vector3 v, Vector3 quant)
    60.     {
    61.         float x = quant.x * Mathf.Floor(v.x / quant.x);
    62.         float y = quant.y * Mathf.Floor(v.y / quant.y);
    63.         float z = quant.z * Mathf.Floor(v.z / quant.z);
    64.         return new Vector3(x, y, z);
    65.     }
    66.  
    67.     Bounds QuantizedBounds()
    68.     {
    69.         // Quantize the bounds to update only when theres a 10% change in size
    70.         var center = m_Tracked ? m_Tracked.position : transform.position;
    71.         return new Bounds(Quantize(center, 0.1f * m_Size), m_Size);
    72.     }
    73.  
    74.     void OnDrawGizmosSelected()
    75.     {
    76.         if (m_NavMesh)
    77.         {
    78.             Gizmos.color = Color.green;
    79.             Gizmos.DrawWireCube(m_NavMesh.sourceBounds.center, m_NavMesh.sourceBounds.size);
    80.         }
    81.  
    82.         Gizmos.color = Color.yellow;
    83.         var bounds = QuantizedBounds();
    84.         Gizmos.DrawWireCube(bounds.center, bounds.size);
    85.  
    86.         Gizmos.color = Color.green;
    87.         var center = m_Tracked ? m_Tracked.position : transform.position;
    88.         Gizmos.DrawWireCube(center, m_Size);
    89.     }
    90. }
    why characters don't move together in build but they move together in the editor? and how to fix it?
     
    Last edited: Mar 11, 2023