I think there are 2 bugs with the RecastGraph’s snap bounds to scene.
1] Very often after snapping to bounds the scene with a recastGraph is just a tiny bit amount too small for it to properly detect all the elements of the scene.
This is a easy fix, you just need to add a small value to the size:
public void SnapForceBoundsToScene () {
var meshes = CollectMeshes(new Bounds(Vector3.zero, new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity)));
if (meshes.Count == 0) {
return;
}
var bounds = meshes[0].bounds;
for (int i = 1; i < meshes.Count; i++) {
bounds.Encapsulate(meshes[i].bounds);
meshes[i].Pool();
}
forcedBoundsCenter = bounds.center;
forcedBoundsSize = bounds.size;
// MY CODE
forcedBoundsSize += Vector3.one;
}
2] Now there’s a more annoying bug when I use Unity’s Terrain, the bounds of the scene are not properly detected.
As you can see the bounds don’t match the scene. This is after clicking “Snap Bounds to Scene”
3] This is probably not a bug but I would like to Snap the bounds to scene as soon as I add my Pathfinding prefab to my scene.
So I made a little button that adds the prefab in the active scene.
But the AstarPath is not finding any of the graphs so I can’t snap bounds to scene. Do you mind telling me why this happens?
public void EditorInit()
{
Debug.Log("Editor init");
var pathfinder = GetComponentInChildren<AstarPath>(true);
if (pathfinder != null)
{
Debug.Log("found pathfinder");
bool hasSnapForced = false;
var graphs = pathfinder.graphs; // ERROR: NOT FINDING ANY GRAPHS DESPITE THE FACT THAT THERE IS ONE IN THE INSPECTOR!!!
for (var i = 0; i < graphs.Length; i++)
{
var navGraph = graphs[i];
Debug.Log($"navgraph #{i}");
if (navGraph is RecastGraph recastGraph)
{
Debug.Log("found recastGraph");
recastGraph.SnapForceBoundsToScene();
hasSnapForced = true;
}
}
if (hasSnapForced)
{
Debug.Log($"Pathfinding: snapped to scene bounds");
}
}
}