public GameObjectRayHit RaycastMeshObject(Ray ray, GameObject gameObject) { EditorMesh editorMesh = EditorMeshDb.Get.GetEditorMesh(gameObject.GetMesh()); if (editorMesh != null) { MeshRayHit meshRayHit = editorMesh.Raycast(ray, gameObject.transform.localToWorldMatrix); if (meshRayHit != null) { return(new GameObjectRayHit(ray, gameObject, meshRayHit)); } } else { // If no EditorMesh instance is available, we will cast a ray against // the object's MeshCollider as a last resort. This is actually useful // when dealing with static mesh objects. These objects' meshes have // their 'isReadable' flag set to false and can not be used to create // an EditorMesh instance. Thus a mesh collider is the next best choice. MeshCollider meshCollider = gameObject.GetComponent <MeshCollider>(); if (meshCollider != null) { RaycastHit rayHit; if (meshCollider.Raycast(ray, out rayHit, float.MaxValue)) { return(new GameObjectRayHit(ray, rayHit)); } } } return(null); }
public GameObjectRayHit(Ray ray, GameObject hitObject, MeshRayHit meshRayHit) { _hitObject = hitObject; _hitPoint = meshRayHit.HitPoint; _hitEnter = meshRayHit.HitEnter; _hitNormal = meshRayHit.HitNormal; _hitPlane = new Plane(_hitNormal, _hitPoint); _meshRayHit = meshRayHit; }