-1

I'm currently working on a 3D procedural dungeon generator with enemy AI's. How do I fix path-finding for the enemy AI's in this situation?

It's not possible to bake navmesh on run-time, and the dungeon is generated differently every time it is runned. The dungeon is generated by prefabs of junctions, rooms and corridors. The enemies cannot move without a navmesh. I created the AI with Panda Behaviour Tree. One AI is supposed to follow a path that is set with waypoints and it runs around when it sees the player. The other AI wanders around the map to search for the player.

The dungeon is generated in the class shown below. I have another class that draws the Gizmos on the doorway of every prefab, and another class that returns the 'ModuleConnector'.

public class ModularWorldGenerator : MonoBehaviour {

public Module[] Modules;
public Module StartModule;
public int Iterations = 5;

public void Start() {
    var startModule = (Module) Instantiate(StartModule, transform.position, transform.rotation);
    var pendingExits = new List<ModuleConnector>(startModule.GetExits());

    for (int iteration = 0; iteration < Iterations; iteration++) {
        var newExits = new List<ModuleConnector>();

        foreach (var pendingExit in pendingExits) {
            var newTag = GetRandom(pendingExit.Tags);
            var newModulePrefab = GetRandomWithTag(Modules, newTag);
            var newModule = (Module) Instantiate(newModulePrefab);
            var newModuleExits = newModule.GetExits();
            var exitToMatch = newModuleExits.FirstOrDefault(x => x.IsDefault) ?? GetRandom(newModuleExits);
            MatchExits(pendingExit, exitToMatch);
            newExits.AddRange(newModuleExits.Where(e => e != exitToMatch));
        }

        pendingExits = newExits;
    }
}


private void MatchExits(ModuleConnector oldExit, ModuleConnector newExit) {
    var newModule = newExit.transform.parent;
    var forwardVectorToMatch = -oldExit.transform.forward;
    var correctiveRotation = Azimuth(forwardVectorToMatch) - Azimuth(newExit.transform.forward);
    newModule.RotateAround(newExit.transform.position, Vector3.up, correctiveRotation);
    var correctiveTranslation = oldExit.transform.position - newExit.transform.position;
    newModule.transform.position += correctiveTranslation;
}


private static TItem GetRandom<TItem>(TItem[] array) {
    return array[Random.Range(0, array.Length)];
}


private static Module GetRandomWithTag(IEnumerable<Module> modules, string tagToMatch) {
    var matchingModules = modules.Where(m => m.Tags.Contains(tagToMatch)).ToArray();
    return GetRandom(matchingModules);
}


private static float Azimuth(Vector3 vector) {
    return Vector3.Angle(Vector3.forward, vector) * Mathf.Sign(vector.x);
}
}

The AI's work both fine in a map that is not random generated and has navmesh baked. How can I fix path-finding for the AI's in this procedural generated dungeon?

1 Answers1

0

I'm fairly sure it's possible to bake at runtime from 2017.1 onwards

https://unity3d.com/learn/tutorials/topics/navigation/baking-navmesh-runtime

The documentation is a little spotty but it should work. It's a relatively new feature.

Empty
  • 506
  • 3
  • 12