private void RaycastAllTerrainObjects(Ray ray, List <GameObjectRayHit> terrainHits) { // Can we pick terrains? if (!IsObjectPickMaskFlagSet(MouseCursorObjectPickFlags.ObjectTerrain)) { // We will use Unity's 'Physics' API for terrain picking because it is reasonable enough // to expect users to attach terrain colliders to their terrain objects. RaycastHit[] rayHits = Physics.RaycastAll(ray); if (rayHits.Length != 0) { // Identify all terrain colliders which were picked foreach (RaycastHit rayHit in rayHits) { // Picked a terrain collider? if (rayHit.collider.GetType() == typeof(TerrainCollider)) { // Create a game object hit instance and add it to the list var terrainRayHit = new TerrainRayHit(ray, rayHit); var gameObjectRayHit = new GameObjectRayHit(ray, rayHit.collider.gameObject, null, null, terrainRayHit, null); terrainHits.Add(gameObjectRayHit); } } } } }
public GameObjectRayHit RaycastAllTerainsClosest(Ray ray) { var terrainHits = new List <GameObjectRayHit>(); RaycastHit[] rayHits = Physics.RaycastAll(ray); if (rayHits.Length != 0) { foreach (RaycastHit rayHit in rayHits) { if (rayHit.collider.GetType() == typeof(TerrainCollider)) { var terrainRayHit = new TerrainRayHit(ray, rayHit); var gameObjectRayHit = new GameObjectRayHit(ray, rayHit.collider.gameObject, null, null, terrainRayHit, null); terrainHits.Add(gameObjectRayHit); } } } if (terrainHits.Count == 0) { return(null); } terrainHits.Sort(delegate(GameObjectRayHit h0, GameObjectRayHit h1) { return(h0.HitEnter.CompareTo(h1.HitEnter)); }); return(terrainHits[0]); }
public GameObjectRayHit(Ray ray, GameObject hitObject, OrientedBoxRayHit objectBoxHit, MeshRayHit objectMeshHit, TerrainRayHit objectTerrainHit, SpriteRayHit objectSpriteHit) { _ray = ray; _hitObject = hitObject; // Only one kind of entity can be registered as a hit, so we will take the first // non-null hit instance using the following priority: terrain, mesh, sprite, box. if (objectTerrainHit != null) { _objectTerrainHit = objectTerrainHit; _objectBoxHit = null; _objectMeshHit = null; _objectSpriteHit = null; } else if (objectMeshHit != null) { _objectTerrainHit = null; _objectBoxHit = null; _objectMeshHit = objectMeshHit; _objectSpriteHit = null; } else if (objectSpriteHit != null) { _objectTerrainHit = null; _objectBoxHit = null; _objectMeshHit = null; _objectSpriteHit = objectSpriteHit; } if (objectBoxHit != null) { _objectTerrainHit = null; _objectBoxHit = objectBoxHit; _objectMeshHit = null; _objectSpriteHit = null; } }