public List <GameObjectRayHit> RaycastAllMesh(Ray ray) { if (!RuntimeEditorApplication.Instance.UseUnityColliders) { return(_gameObjectSphereTree.RaycastAllMesh(ray)); } else { RaycastHit[] hits = Physics.RaycastAll(ray); var gameObjectHits = new List <GameObjectRayHit>(); foreach (var hit in hits) { // Retrieve the object which was hit GameObject gameObject = hit.collider.gameObject; if (gameObject == null) { continue; } if (!gameObject.activeSelf) { continue; } GameObjectRayHit gameObjectRayHit = null; if (gameObject.RaycastMesh(ray, out gameObjectRayHit)) { gameObjectHits.Add(gameObjectRayHit); } } return(gameObjectHits); } }
public static bool RaycastBox(this GameObject gameObject, Ray ray, out GameObjectRayHit objectRayHit) { objectRayHit = null; OrientedBox objectWorldOrientedBox = gameObject.GetWorldOrientedBox(); OrientedBoxRayHit objectBoxRayHit; if (objectWorldOrientedBox.Raycast(ray, out objectBoxRayHit)) { objectRayHit = new GameObjectRayHit(ray, gameObject, objectBoxRayHit, null, null, null); } return(objectRayHit != null); }
public static bool RaycastMesh(this GameObject gameObject, Ray ray, out GameObjectRayHit objectRayHit) { objectRayHit = null; Mesh objectMesh = gameObject.GetMeshFromFilterOrSkinnedMeshRenderer(); if (objectMesh == null) { return(false); } EditorMesh editorMesh = EditorMeshDatabase.Instance.GetEditorMesh(objectMesh); if (editorMesh != null) { MeshRayHit meshRayHit = editorMesh.Raycast(ray, gameObject.transform.GetWorldMatrix()); if (meshRayHit == null) { return(false); } objectRayHit = new GameObjectRayHit(ray, gameObject, null, meshRayHit, null, null); return(true); } else { MeshCollider meshCollider = gameObject.GetComponent <MeshCollider>(); if (meshCollider != null) { RaycastHit rayHit; if (meshCollider.Raycast(ray, out rayHit, float.MaxValue)) { MeshRayHit meshRayHit = new MeshRayHit(ray, rayHit.distance, rayHit.triangleIndex, rayHit.point, rayHit.normal); objectRayHit = new GameObjectRayHit(ray, gameObject, null, meshRayHit, null, null); return(true); } } else { return(gameObject.RaycastBox(ray, out objectRayHit)); } } return(false); }
/// <summary> /// Performs a raycast and returns a list of hits for all sprite objects intersected /// by the ray. /// </summary> public List <GameObjectRayHit> RaycastAllSprite(Ray ray) { // First, retrieve a list of the sphere tree nodes which were hit by the ray. // If no nodes were hit, it means no object was hit either. List <SphereTreeNodeRayHit <GameObject> > allNodeHits = _sphereTree.RaycastAll(ray); if (allNodeHits.Count == 0) { return(new List <GameObjectRayHit>()); } // Loop through all nodes which were hit by the ray. For each node, we have to detect // if the ray hits the sprite object. var gameObjectHits = new List <GameObjectRayHit>(); foreach (SphereTreeNodeRayHit <GameObject> nodeHit in allNodeHits) { // Retrieve the object which resides in the node GameObject gameObject = nodeHit.HitNode.Data; if (gameObject == null) { continue; } if (!gameObject.HasSpriteRendererWithSprite()) { continue; } // If the ray intersects the object's sprite, add the hit to the list GameObjectRayHit gameObjectRayHit = null; if (gameObject.RaycastSprite(ray, out gameObjectRayHit)) { gameObjectHits.Add(gameObjectRayHit); } } return(gameObjectHits); }
public static bool RaycastSprite(this GameObject gameObject, Ray ray, out GameObjectRayHit objectRayHit) { objectRayHit = null; SpriteRenderer spriteRenderer = gameObject.GetComponent <SpriteRenderer>(); if (spriteRenderer == null) { return(false); } OrientedBox objectWorldOrientedBox = gameObject.GetWorldOrientedBox(); OrientedBoxRayHit objectBoxRayHit; if (objectWorldOrientedBox.Raycast(ray, out objectBoxRayHit)) { SpriteRayHit spriteHit = new SpriteRayHit(ray, objectBoxRayHit.HitEnter, spriteRenderer, objectBoxRayHit.HitPoint, objectBoxRayHit.HitNormal); objectRayHit = new GameObjectRayHit(ray, gameObject, null, null, null, spriteHit); } return(objectRayHit != null); }
/// <summary> /// Performs a raycast and returns a list of hits for all objects whose meshes /// are intersected by the specified ray. /// </summary> public List <GameObjectRayHit> RaycastAllMesh(Ray ray) { // First, we will gather the objects whos boxes are intersected by the ray. If // no such objects exist, we will return an empty list. List <GameObjectRayHit> allBoxHits = RaycastAllBox(ray); if (allBoxHits.Count == 0) { return(new List <GameObjectRayHit>()); } // Now we will loop through all these objects and identify the ones whose meshes // are hit by the ray. var allMeshObjectHits = new List <GameObjectRayHit>(allBoxHits.Count); foreach (var boxHit in allBoxHits) { // Store the object for easy access GameObject hitObject = boxHit.HitObject; if (hitObject == null) { continue; } if (!hitObject.activeSelf) { continue; } GameObjectRayHit gameObjectRayHit = null; if (hitObject.RaycastMesh(ray, out gameObjectRayHit)) { allMeshObjectHits.Add(gameObjectRayHit); } } return(allMeshObjectHits); }