/// <summary> /// Initialises the component. /// </summary> public void Start() { this.planeZ = new Plane(Vector3.back, Vector3.zero); // Get a refernce to the terrain's touchable component this.terrainTouchable = GameObject.Find("Terrain").GetComponent<TouchableComponent>(); }
/// <summary> /// Get the component that lies at the given screen position. /// </summary> /// <param name="touchPosition">The screen position of the touch.</param> /// <param name="touchable">The touchable component.</param> /// <param name="hitPoint">The point at which the component was touched in world coordinates.</param> /// <returns>The touchable component; Null if touchable object exists at the position.</returns> private bool TryGetTouchedComponent(Vector2 touchPosition, out TouchableComponent touchable, out Vector3 hitPoint) { // Cast a ray into the scene at the touched point Ray ray = Camera.main.ScreenPointToRay(touchPosition); // See if an actor was hit // TODO // No actors were hit. See where the ray hits the terrain on the Z = 0 plane float distance; if (this.planeZ.Raycast(ray, out distance)) { touchable = this.terrainTouchable; hitPoint = ray.GetPoint(distance); return true; } touchable = null; hitPoint = Vector3.zero; return false; }