Hello,
My map is procedurally generated, and I want to use the navigation system. I’m trying to bake the NavMesh at runtime. But my code isn’t working.
private int planeScale = 5;
private List<NavMeshBuildSource> sources;
public GameObject prefab;
void Start () {
Vector3 scale = transform.localScale * planeScale;
sources = new List<NavMeshBuildSource> ();
for (int i = 0; i < (int) scale.x; i++) {
for (int j = 0; j < (int) scale.z; j++) {
if (Random.value > seuil) {
Vector3 pos = new Vector3 (2f * i - scale.x, 1.0f, 2f * j - scale.z);
Instantiate (prefab, pos, Quaternion.identity);
NavMeshBuildSource src = new NavMeshBuildSource ();
src.transform = transform.localToWorldMatrix;
src.shape = NavMeshBuildSourceShape.Box;
src.size = prefab.transform.localScale;
sources.Add (src);
}
}
}
NavMeshBuildSettings settings = new NavMeshBuildSettings ();
string[] res = settings.ValidationReport (GetComponent<MeshCollider>().bounds);
Debug.Log (res); // Return an empty String array
NavMeshData data = NavMeshBuilder.BuildNavMeshData(settings, sources, new Bounds(), Vector3.zero, Quaternion.identity);
Debug.Log (data); // Isn't null
NavMeshDataInstance inst = NavMesh.AddNavMeshData (data);
Debug.Log (inst.valid); // Display true
}
If in the editor I first bake the nav mesh manually the previous code doesn’t modify the nav mesh.
But if I don’t bake it first, I got this warning :
Failed to create agent because it is not close enough to the NavMesh
UnityEngine.AI.NavMesh:AddNavMeshData(NavMeshData)
I’m missing something?
Thanks for help.
EDIT: I’m using Unity 5.6.0b11