Physics.Boxcast not working with mesh collider

Hi

I’ve been trying to make Physics.Boxcast work and spent a few hours fiddling with it without much success, until I discovered that it apparently ignores mesh colliders. I’ve tried replacing my mesh with a box collider and then everything starts working like a charm; but I really need my collider to have the same shape as my mesh, so a box collider will just not work.

Does anyone know if this is a known issue? May I be doing something wrong?

My code does the following: It gets two items, a terrain like(but not a unity terrain) mesh (target) and a cube clipping with that mesh in y axis (origin). Then it trows a boxcast in the containing box of the cube to detect the lowest point of the mesh, still in the cube’s bouding area; I’m getting the lowest point of the mesh located below the cube.

public static float LocateLowestCollidingSection(GameObject origin, GameObject target){
		

		Mesh originMesh = origin.GetComponent<MeshFilter>().mesh;
		RaycastHit hit;
		float searchMargin = 50f;

		if (Physics.BoxCast (new Vector3 (origin.transform.position.x, origin.transform.position.y - searchMargin, //- originMesh.bounds.extents.y/2f, 
			origin.transform.position.z),
			new Vector3 (originMesh.bounds.extents.x, 0.1f, originMesh.bounds.extents.z), 
			Vector3.up, out hit, Quaternion.identity, searchMargin + originMesh.bounds.extents.y, ~target.layer)){

			return hit.point.y;
		} else {
			
			Debug.LogError ("And thus, only ethernal void remains below the target.");
			return Mathf.Infinity;
		}
	}

Welp, turns out it was indeed my fault.

Neither boxCast nor rayCast will hit a mesh on its backside, and since my mesh needs to be upwards (because I need a player to be able to walk on it) what needs to be done is either:

Change the way of obtaining that point: Throwing raycasts from the top may be a good approximation, although it might not be precise or be too resource consuming if the mesh is huge and has detail. I’ve not tested this but it should work.

Rotate our mesh before we use the boxcast method and rotate it again after measuring so that the player does not notice the change. Does not sound efficient to me either, but I’ve tried doing this on update and can barely notice the difference (and I will only do this once, not on Update when it’s implemented).

For anyone in the same situation as me, I used carmine’s code to invert my mesh: Sphere collider invert? - Unity Answers