Hi all,
Not sure if this should be in graphics forum or here so apologies of it’s in the wrong place. I have seen a few old posts with a similar issue to this and tried a bunch of things but just can’t figure out why this doesn’t work.
I have a custom generated mesh (a tiled hex map). I want to use a ray to fire straight down onto the map and get the hit point (which will be used to place a tree - the red sphere in the images below).
Here is the code section:
public void CreateTree(MapCell cell)
{
int height = 200;
RaycastHit hit;
Ray ray = new Ray(new Vector3(cell.transform.position.x, height, cell.transform.position.z), Vector3.down);
// Debug version
// RaycastHit hit;
// Ray ray = new Ray(new Vector3(0, 20, 0), Vector3.down);
if (Physics.Raycast(ray, out hit))
{
Debug.Log("## Hit point! " + hit.point);
Tree tree = Instantiate(treePrefab);
tree.transform.position = hit.point;
}
else
{
Debug.LogError("Tree raycast failed");
}
}
Sadly the Ray never hits the mesh
This is a gizmo showing the source and direction of the Ray. Doesn’t hit the mesh…
If I add a plane into the scene above the mesh, the Ray hit the plane just fine. See the “tree” (red sphere) created at the hit point.
If I put a box collider on each hex, the Ray also hits.
But I can’t get the Ray to hit the MeshCollider.
I tried making it Convex, didn’t work. The Cooking Options are set to default, isTrigger is off. I am setting the shared mesh like this:
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = hexMesh;
meshCollider.convex = true;
meshCollider.enabled = true;
I’m sure normals are facing the right way (up). I’ve also tried firing a ray “up” from below the mesh, didn’t work.
Any ideas??
Thanks.