public static bool RaycastTerrain(this GameObject gameObject, Ray ray, out GameObjectRayHit objectRayHit) { objectRayHit = null; if (!gameObject.HasTerrain()) { return(false); } TerrainCollider terrainCollider = gameObject.GetComponent <TerrainCollider>(); if (terrainCollider == null) { return(false); } RaycastHit raycastHit; if (terrainCollider.Raycast(ray, out raycastHit, float.MaxValue)) { TerrainRayHit terrainRayHit = new TerrainRayHit(ray, raycastHit); objectRayHit = new GameObjectRayHit(ray, gameObject, null, null, terrainRayHit, null); } return(objectRayHit != null); }
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(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; } }