Beispiel #1
0
        public GameObjectRayHit RaycastMeshObject(Ray ray, GameObject gameObject)
        {
            Mesh   objectMesh = gameObject.GetMesh();
            RTMesh rtMesh     = RTMeshDb.Get.GetRTMesh(objectMesh);

            if (rtMesh != null)
            {
                MeshRayHit meshRayHit = rtMesh.Raycast(ray, gameObject.transform.localToWorldMatrix);
                if (meshRayHit != null)
                {
                    return(new GameObjectRayHit(ray, gameObject, meshRayHit));
                }
            }
            else
            {
                // If no RTMesh 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 RTMesh 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);
        }
Beispiel #2
0
 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;
 }