【发布时间】:2019-01-09 16:42:06
【问题描述】:
我目前正在开发一个带有敌方 AI 的 3D 程序地牢生成器。 在这种情况下,我该如何解决敌方 AI 的寻路问题?
无法在运行时烘焙 navmesh,并且每次运行都会生成不同的地牢。地牢是由路口、房间和走廊的预制件生成的。 没有导航网,敌人就无法移动。我用熊猫行为树创建了人工智能。一个 AI 应该遵循一条设置了航路点的路径,并在看到玩家时四处奔跑。另一个 AI 在地图上四处游荡以寻找玩家。
地牢在如下所示的类中生成。 我有另一个类在每个预制件的门口绘制 Gizmo,另一个类返回“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);
}
}
AI 在非随机生成的地图和已烘焙的导航网格中都能正常工作。 如何在这个程序生成的地牢中修复 AI 的寻路问题?
【问题讨论】:
标签: c# unity3d artificial-intelligence path-finding procedural-generation