# How to make calculation distance from 1 point to another point in terrain

Can any1 teach me how to display a distance or calculate the distance when i click 1 point on the terrain to another point. or any information around this Thx

If you want to calculate the distance between two points directly, you can use something like this. Put this script on your terrain object:

``````public bool useAdvanced = false;
public int samples = 10;
bool calculatingDistance = false;
Vector3 startPoint;
float distance;

void Update()
{
if(Input.GetMouseButtonDown(0))
{
DoPointSample();
}
}

void DoPointSample()
{
RaycastHit hit;
if(collider.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition), out hit, Mathf.Infinity))
{
if(!calculatingDistance)
{
startPoint = hit.point;
calculatingDistance = true;
SampledDistance(hit.point);
} else {
SimpleDistance(hit.point);
}
}
}

void SimpleDistance(Vector3 endPoint)
{
distance = Vector3.Distance(startPoint, endPoint);
}

void SampledDistance(Vector3 endPoint)
{
float totalDistance = 0;
if(samples <= 0)
{
SimpleDistance(endPoint);
return;
}
Vector3 lastPoint = startPoint;
Terrain localTerrain = GetComponent<Terrain>();
for(int i = 0; i <= samples; ++i)
{
Vector3 nextPoint = localTerrain.SampleHeight(Mathf.Lerp(startPoint, endPoint, (float)i / (float)samples));
distance += Vector3.Distance(lastPoint, nextPoint);
lastPoint = nextPoint;
}
}

void OnGUI()
{
GUILayout.Label("Distance B/W Points: " + distance);
}
``````

You can choose between two different methods by setting the ‘useAdvanced’ button in the inspector.