Topic: How to find a route on terrains using Online Maps API

Example of how to find a route on terrains created in Real World Terrain using Online Maps API.

Script:

/*     INFINITY CODE 2013-2018      */
/*   http://www.infinity-code.com   */

using InfinityCode.RealWorldTerrain;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    public class FindRouteOnTerrainUsingOnlineMaps : MonoBehaviour
    {
        /// <summary>
        /// Real World Terrain Container
        /// </summary>
        public RealWorldTerrainContainer container;

        /// <summary>
        /// Material for the route, and points on terrainsMat
        /// </summary>
        public Material lineRendererMaterial;

        /// <summary>
        /// Index of point (click)
        /// </summary>
        private int pointIndex = 0;

        /// <summary>
        /// Coordinates of points
        /// </summary>
        private Vector2[] points;

        /// <summary>
        /// Instances of points
        /// </summary>
        private GameObject[] pointInstances;

        /// <summary>
        /// Instance of LineRenderer
        /// </summary>
        private LineRenderer lineRenderer;

        /// <summary>
        /// Screen position of last press
        /// </summary>
        private Vector2 pressPoint;

        /// <summary>
        /// This method is called when responding from the Google Directions API
        /// </summary>
        /// <param name="response">Response string</param>
        private void OnGoogleDirectionsComplete(string response)
        {
            OnlineMapsGoogleDirectionsResult result = OnlineMapsGoogleDirections.GetResult(response);
            if (result == null || result.routes.Length == 0) return;

            Vector2[] polyline = result.routes[0].overview_polyline;

            GameObject go = new GameObject("LineRenderer");
            lineRenderer = go.AddComponent<LineRenderer>();
            lineRenderer.sharedMaterial = lineRendererMaterial;
            lineRenderer.widthCurve = AnimationCurve.Constant(0, 1, 2);
            lineRenderer.startColor = lineRenderer.endColor = Color.blue;
            lineRenderer.positionCount = polyline.Length;

            for (int i = 0; i < polyline.Length; i++)
            {
                Vector3 p;
                container.GetWorldPosition(polyline[i], out p);
                lineRenderer.SetPosition(i, p + new Vector3(0, 2, 0));
            }
        }

        /// <summary>
        /// Process click on terrain
        /// </summary>
        private void ProcessClick()
        {
            bool success = container.GetCoordinatesUnderCursor(out points[pointIndex]);
            if (!success) return;

            if (pointIndex == 0)
            {
                if (lineRenderer != null)
                {
                    DestroyImmediate(lineRenderer.gameObject);
                    DestroyImmediate(pointInstances[0]);
                    DestroyImmediate(pointInstances[1]);
                    lineRenderer = null;
                }
            }

            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            Vector3 p;
            container.GetWorldPosition(points[pointIndex], out p);
            go.transform.position = p;
            go.transform.localScale = new Vector3(3, 3, 3);
            go.GetComponent<Renderer>().sharedMaterial = lineRendererMaterial;
            pointInstances[pointIndex] = go;

            pointIndex++;
            if (pointIndex < 2) return;

            pointIndex = 0;

            OnlineMapsGoogleDirections.Find(points[0], points[1]).OnComplete += OnGoogleDirectionsComplete;
        }

        private void Start()
        {
            points = new Vector2[2];
            pointInstances = new GameObject[2];
        }

        private void Update()
        {
            if (Input.GetMouseButtonDown(0)) pressPoint = Input.mousePosition;
            else if (Input.GetMouseButtonUp(0))
            {
                if ((pressPoint - (Vector2) Input.mousePosition).magnitude < 10) ProcessClick();
            }
        }
    }
}
Kind Regards,
Infinity Code Team