Support Forum

Recast Graph BUG: Snap Bounds to scene

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");
        }
    }
}

I’d love to have some answers about this subject :slight_smile:

Hi

I cannot replicate either 1 or 2. I tried to just add a recast graph to “Example2” included in the package and it snaps correctly to the terrain.

Are you sure the layer masks for your graph is correct?

  1. If this is outside of play mode you need to call AstarPath.FindAstarPath to do some initialization logic and deserialize all graphs.

For bug #1: I tested it with a new gamobject with and a new Pathfinder object and it still occured.
Refer to the video here to see the problem occur with a simple ground object: https://www.youtube.com/watch?v=muAccEst_PE&feature=youtu.be

And you’re right about bug #2, it was my mistake: it was because I forgot to have rasterize terrains on.

And thanks for telling me about the 3rd point, it works.