I faced the problem that the scene of my game changes depending on the actions of the player (he can put obstacles) and navmesh agent should take into account these changes and destroy them.
So I need to recalculate paths for navmesh agent right in the time of the game, I found the function navMeshSurface.BuildNavMeshAsync(); in the hope that I can bake and the game would not stop. When this method is called the game freezes.
What could be the problem with asynchrony?
Perhaps there is an alternative to rebaking, please give me ideas
Last iteration
private async void Update()
{
if(Input.GetKeyDown(KeyCode.N))
{
SpawnNextAtRandomSpawner();
navMeshSurface = GameObject.Find("Navmesh").GetComponent<NavMeshSurface>();
await UpdateNavMeshAsync();
}
}
async Task UpdateNavMeshAsync()
{
navMeshSurface.RemoveData();
navMeshSurface.BuildNavMeshAsync(); //Freeze moment
}
You are over thinking it way too much
so this link takes you to a video of the below in action.
public class spawner : MonoBehaviour
{
public int Delay = 5;
public GameObject cube;
public NavMeshSurface surface;
private float nextspawntime = 0;
Vector3 newpos()
{
return new Vector3(Random.Range(-24,24),0,Random.Range(-24,24));
}
void Update()
{
nextspawntime += Time.deltaTime;
if (nextspawntime> Delay)
{
nextspawntime = 0;
Instantiate(cube, newpos(), Quaternion.identity);
}
}
}