Support Forum

Layered Grid Graph not baking multiple height layers if the ground is a single mesh


Had a quick question about the layered grid graph. I’m working on a game where a procedural cave system is generated and would like to use the layered grid graph to handle multiple stories/levels. However, since the cave floor is a single mesh (one mesh collider), it looks like layered grid graph is only baking on the topmost layer? Any tips on this? The ground is on the terrain layer which the graph is looking for and obstacles are disabled just to show that’s not the issue. Here’s a screenshot of the baked graph and my settings:

Any help is greatly appreciated! Thanks!

and here’s the same exact setup but with a shorter ray length. Just to show that the bottom layer is able to bake. Although this setup obviously introduces the opposite issue (now the top layer has no baked graph):

I think I was able to fix it by modifying CheckHeightAll in Base.cs:

		public RaycastHit[] CheckHeightAll (Vector3 position, out int numHits) {
			if (!heightCheck || use2D) {
				hitBuffer[0] = new RaycastHit {
					point = position,
					distance = 0,
				numHits = 1;
				return hitBuffer;

			// Cast a ray from above downwards to try to find the ground
#if UNITY_2017_1_OR_NEWER
			// numHits = Physics.RaycastNonAlloc(position+up*fromHeight, -up, hitBuffer, fromHeight+0.005F, heightMask, QueryTriggerInteraction.Ignore);
			// if (numHits == hitBuffer.Length) {
			// 	// Try again with a larger buffer
			// 	hitBuffer = new RaycastHit[hitBuffer.Length*2];
			// 	return CheckHeightAll(position, out numHits);
			// }
			// return hitBuffer;

			RaycastHit hit;
			List<RaycastHit> hits = new List<RaycastHit>();
			int count = 0;
			float distanceTraveled = 0;
			Vector3 startPos = position+up*fromHeight;
			int maxLoops = 100;

			while (Physics.Raycast(startPos, -up, out hit, (fromHeight + 0.005F) - distanceTraveled, heightMask,
			       && count < maxLoops)
				startPos = hit.point + -up * .05f;		//Small offset to prevent hitting same spot
				distanceTraveled += hit.distance;

			numHits = count;
			hitBuffer = hits.ToArray();
			return hitBuffer;

			var result =
 Physics.RaycastAll(position+up*fromHeight, -up, fromHeight+0.005F, heightMask, QueryTriggerInteraction.Ignore);
			numHits = result.Length;
			return result;


This has also been fixed in the beta, I believe. See A* Pathfinding Project

1 Like