- Home /
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;
}
}
Hmmm... not sure entirely, but here's two things to know that may lead you to an answer:
First, is that mesh colliders will not collide with other mesh colliders. So, they are special in some ways, but I don't see anything in the docs that would indicate BoxCast wouldn't work.
Second, the raycast/boxcast docs say that if the origin is within the collider's bounds, it won't trigger a collision (e.g. a raycast starting in the middle of a sphere and moving out won't hit the outside of the sphere). So maybe you need to move back your origin point to ensure it's outside of the mesh collider's bounds?
I've tried moving the cast away from the mesh, in fact in my example I use a huge margin of 50f, while both my target and origin are both (0,0,0). The fact that boxcast will not work if already inside the object makes me think that the fact that the mesh collider is not convex might have something to do with that.
But as far as I remember raycasts worked fine when I tried them... I'll try a couple of things and comment again with the conclusions, still think it might be an issue with boxcast.
Answer by Whipexx · May 04, 2016 at 01:24 PM
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: http://answers.unity3d.com/questions/213427/sphere-collider-invert.html